Can I avoid eager ambiguity resolution for trait implementations with generics?

Can I avoid eager ambiguity resolution for trait implementations with generics? No. Is it possible to have [ambiguity resolution to be done at the call site, rather than at the definition site]? No. There’s a (long-delayed) RFC for specialization that will allow overlapping trait implementations, but only when one of them is more specific than … Read more

Wrong specialized generic function gets called in Swift 3 from an indirect call

This is indeed correct behaviour as overload resolution takes place at compile time (it would be a pretty expensive operation to take place at runtime). Therefore from within test(value:), the only thing the compiler knows about value is that it’s of some type that conforms to DispatchType – thus the only overload it can dispatch … Read more

Initialize Java Generic Array of Type Generic

Generics in Java doesn’t allow creation of arrays with generic types. You can cast your array to a generic type, but this will generate an unchecked conversion warning: public class HashTable<K, V> { private LinkedList<V>[] m_storage; public HashTable(int initialSize) { m_storage = (LinkedList<V>[]) new LinkedList[initialSize]; } } Here is a good explanation, without getting into … Read more

How do I add different types conforming to a protocol with an associated type to a collection?

Protocols with type aliases cannot be used this way. Swift doesn’t have a way to talk directly about meta-types like ValidationRule or Array. You can only deal with instantiations like ValidationRule where… or Array<String>. With typealiases, there’s no way to get there directly. So we have to get there indirectly with type erasure. Swift has … Read more

Java collections covariance problem

You’re probably going to need to take a look at using wildcard types for generics. Here’s a quick link: What is PECS (Producer Extends Consumer Super)? Quick answer: change the type to List<? extends AbstractItem> Why can’t you just assign this? Imagine the code here… List<AbstractItem> foo = new ArrayList<SharpItem>(); foo.add(new BluntItem()); The static typing … Read more