covariance
Why is parameter in contravariant position?
This is a fundamental feature of object-oriented programming that doesn’t get as much attention as it deserves. Suppose you have a collection C[+T]. What the +T means is that if U <: T, then C[U] <: C[T]. Fair enough. But what does it mean to be a subclass? It means that every method should work … Read more
Difference between Variance, Covariance, Contravariance, Bivariance and Invariance in TypeScript
Variance has to do with how a generic type F<T> varies with respect to its type parameter T. If you know that T extends U, then variance will tell you whether you can conclude that F<T> extends F<U>, conclude that F<U> extends F<T>, or neither, or both. Covariance means that F<T> and T co–vary. That … Read more
How to get around lack of covariance with IReadOnlyDictionary?
You could write your own read-only wrapper for the dictionary, e.g.: public class ReadOnlyDictionaryWrapper<TKey, TValue, TReadOnlyValue> : IReadOnlyDictionary<TKey, TReadOnlyValue> where TValue : TReadOnlyValue { private IDictionary<TKey, TValue> _dictionary; public ReadOnlyDictionaryWrapper(IDictionary<TKey, TValue> dictionary) { if (dictionary == null) throw new ArgumentNullException(“dictionary”); _dictionary = dictionary; } public bool ContainsKey(TKey key) { return _dictionary.ContainsKey(key); } public IEnumerable<TKey> Keys … Read more
Covariance and Overloading in Java
can you please explain the first line last false output, why it is not true? The compile-time type of ab is A, so the compiler – which is what performs overload resolution – determines that the only valid method signature is f(A a), so it calls that. At execution time, that method signature is executed … Read more
What is the difference between covariance and contra-variance in programming languages? [closed]
Covariance is pretty simple and best thought of from the perspective of some collection class List. We can parameterize the List class with some type parameter T. That is, our list contains elements of type T for some T. List would be covariant if S is a subtype of T iff List[S] is a subtype … Read more
Why can’t I cast from a List to List?
The reason this is not legal is because it is not safe. Suppose it were legal: List<Giraffe> giraffes = new List<Giraffe>(); List<Animal> animals = giraffes; // this is not legal; suppose it were. animals.Add(new Tiger()); // it is always legal to put a tiger in a list of animals But “animals” is actually a list … Read more
Make dictionary read only in C#
It would be as easy as casting the whole dictionary reference to IReadOnlyDictionary<string, IReadOnlyList<string>> because Dictionary<TKey, TValue> implements IReadOnlyDictionary<TKey, TValue>. BTW, you can’t do that because you want the List<string> values as IReadOnlyList<string>. So you need something like this: var readOnlyDict = (IReadOnlyDictionary<string, IReadOnlyList<string>>)dict .ToDictionary(pair => pair.Key, pair => pair.Value.AsReadOnly()); Immutable dictionaries This is just … Read more
When is C++ covariance the best solution?
The canonical example is a .clone()/.copy() method. So you can always do obj = obj->copy(); regardless what obj’s type is. Edit: This clone method would be defined in the Object base class (as it actually is in Java). So if clone wasn’t covariant, you would either have to cast, or would be restricted to methods … Read more
Why doesn’t this generic extension method compile?
Quoting the C# specification: 7.6.5.2 Extension method invocations In a method invocation (ยง7.5.5.1) of one of the forms expr . identifier ( ) expr . identifier ( args ) expr . identifier < typeargs > ( ) expr . identifier < typeargs > ( args ) if the normal processing of the invocation finds no … Read more