generics

how to circumvent Kotlin's restriction “Type parameter is forbidden for catch parameter”

≯℡__Kan透↙ 提交于 2020-01-16 05:04:46
问题 I have defined the following function: inline fun <T> T.tryTo(block: T.() -> Unit): T? { try { block() } catch (ex: IllegalArgumentException) { return this } return null } The purpose is to build a chain of try-actions on an object, e.g.: val input: String = getInput(); input.tryTo /* treat as a file name and open the file */ { Desktop.getDesktop().open(File(this)) }?.tryTo /* treat as a number */ { try { doSomethingWithTheNumber(parseInt(this)) } catch (ex: NumberFormatException) { throw

Type Inference between generics with reversed constraints

北城余情 提交于 2020-01-16 04:37:12
问题 This is an extension from this question, which had an answer that works in that specific case. My actual code looks more like this: public abstract class BaseComparable<TLeft, TRight> { } public class LeftComparable<TLeft, TRight> : BaseComparable<TLeft, TRight> where TLeft : IComparable<TRight> { public LeftComparable(TLeft value) { } } public class RightComparable<TLeft, TRight> : BaseComparable<TLeft, TRight> where TRight : IComparable<TLeft> { public RightComparable(TLeft value) { } } If

Unchecked cast warnings in generic method on parameter that must be used in a List

北战南征 提交于 2020-01-16 03:12:30
问题 In the code below, the type parameter D can be either a List<Byte> or a List<List<Byte>> (it is the third generic parameter in the Fields<?, ?, D> interface but still I might omit it there - but it is present also in the return type of the method). Can't seem to find a way to tell the compiler this - get Unchecked cast warnings in the lines marked //* : public static <D, K, T extends Enum<T> & Fields<?, ?, D>> List<EnumMap<T, D>> getEntries(InputStream is, Class<T> fields) throws IOException

What are the use cases of having new() in “public T SomeMethod<T>(string item) where T : new();” [closed]

断了今生、忘了曾经 提交于 2020-01-16 01:01:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I am trying to identify various uses cases of using new() in statement public T SomeMethod<T>(string item) where T : new(); I know compiler will ensure that T must have a default constructor. But in what all scenario this is helpful. I have gone through this link 回答1: MSDN's own

System.Collections.Generic.KeyNotFoundException with Mono, but not Microsoft's .NET

[亡魂溺海] 提交于 2020-01-16 00:57:45
问题 I found a really strange problem while creating unit tests that only occurs with the Mono runtime (Xamarin on Mac included), but runs fine within Visual Studio. I isolated it as far as I could, and I reached a point that I can't tell if it is a bug with Mono, Moq or Castle DinamicProxy, although it only crashes when using the Mono runtime. This is the code: using System; using System.Collections.Generic; using Moq; namespace ConsoleApplication1 { public interface ISomething<T> { List<T>

what is the importance of <Integer,Integer> in Map.Entry.<Integer, Integer>comparingByValue()

会有一股神秘感。 提交于 2020-01-16 00:41:24
问题 I am trying to sort elements by their frequency import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int itr = Integer.parseInt(br.readLine()); for (int i = 0; i < itr; i++) { int n = Integer.parseInt(br.readLine()); String[] val = br.readLine

Converting from closed generics to open generics

倾然丶 夕夏残阳落幕 提交于 2020-01-15 23:25:48
问题 I am looking for a way to convert the following closed generics statement to open generic statement, i.e. I don't want repeat the same for entities like User, Employer, etc. Closed type using User: UnityContainer.RegisterType<Func<IDataContextAdapter, IRepository<User>>>( new InjectionFactory(c => new Func<IDataContextAdapter, IRepository<User>>( context => new Repository<User>(context)) ) ); I tried converting to Open generics by applying typeof(..) operation, but didn't had much success.

Declare a Binary Tree which takes a Generic type of Node which contains a Generic type of key value?

China☆狼群 提交于 2020-01-15 19:14:17
问题 If I have a node class(es) that can accept a generic type for it's key value: class Node<K extends Comparable<K>> implements Comparable<Node<K> { ... } class KeyValueNode<K extends Comparable<K>, V> extends Node<K> { ... } Is it possible to declare a generic binary tree class that accepts a generic type of node, which can contain a generic type of key value? I thought it would look something like this.... class BinaryTree<N<K>> { N<K> root; BinaryTree<N<K>> left, right; ... } Apologies for

Java - An interface that has static field pointing to class name of its sub class?

别说谁变了你拦得住时间么 提交于 2020-01-15 11:08:49
问题 I want an interface, which its sub class can inherit a static field, that field points to the name of the sub class. How can I do that? For example in my mind (the code is unusable): public interface ILogger<A> { public static String LogTag = A.class.getName(); } public class Sub implements ILogger<Sub> { public Sub() { Log.debug(LogTag, ...); } } 回答1: In Java, unlike C++, this is not possible due to the way that generics are implemented. In Java, there is only one class for each generic type

Scala Type mismatch when a generic type operates on the same generic type

主宰稳场 提交于 2020-01-15 10:55:09
问题 I have an generic case class Route that takes in a List of subclasses of Location. However in the following method I get a type mismatch in the call to distance expected: head.T, actual: T case class Route[T <: Location](route: List[T]) { def measureDistance: Double = { def measure(head: T, tail: List[T], acc: Double = 0.0): Double = tail match { case Nil => acc case h :: t => measure(h, t, head.distance(h) + acc) } if (route.isEmpty) 0.0 else measure(route.head, route.tail) } } The basic