Why does C # (4.0) not allow coincidence and contravariance in types of a universal class?

What is the real reason for this limitation? Is this just a job to do? Is it conceptually difficult? It's impossible?

Of course, you cannot use type parameters in fields, because they always read and write. But that can't be the answer, can it?

The reason for this question is because I am writing an article on dispersion support in C # 4, and I feel that I have to explain why it is limited to delegates and interfaces. Just to pay attention to the burden of proof.

Update: Eric asked about an example.

How about this (I don't know if that makes sense, but :-))

public class Lookup<out T> where T : Animal { public T Find(string name) { Animal a = _cache.FindAnimalByName(name); return a as T; } } var findReptiles = new Lookup<Reptile>(); Lookup<Animal> findAnimals = findReptiles; 

The reason for this in one class may be the cache that is stored in the class itself. And please do not call your animals of different types the same!

By the way, this leads me to optional type parameters in C # 5.0 :-)

Update 2: I am not saying that the CLR and C # should allow this. Just trying to understand what led to the fact that this is not so.

+22
generics c # covariance contravariance
Mar 29 '10 at 21:40
source share
3 answers

First of all, as Thomas says, it is not supported in the CLR.

Secondly, how will this work? Suppose you have

 class C<out T> { ... how are you planning on using T in here? ... } 

T can only be used in output positions. As you noticed, a class cannot have any field of type T, because the field can be written. A class cannot have any methods that accept T because they are logically written. Suppose you had this feature - how would you use it?

This would be useful for immutable classes if we could, for example, make it legal to have a read-only field of type T; thus, we would significantly reduce the likelihood that it would be spelled incorrectly. But itโ€™s rather difficult to come up with other scripts that allow type deviations.

If you have such a scenario, I would like to see it. This will mean that someday it will be implemented in the common language runtime.

UPDATE: see

Why is there no general variance for classes in C # 4.0?

for more information on this.

+20
Mar 29 '10 at 21:53
source share

As far as I know, this function is not supported by the CLR, so adding this will require considerable effort on the CLR side. I believe that the joint and opposite variance for interfaces and delegates was actually supported in the CLR prior to version 4.0, so this was a relatively simple extension to implement.

(Supporting this function for classes will definitely be useful!)

+8
Mar 29 '10 at 21:45
source share

If they were allowed, it would be possible to define useful classes or structures with a power-safe (without internal types) 100% that would be covariant with respect to their type T if their constructor accepted one or more suppliers of T or T. Useful, 100 % -type classes or structures can be defined that were contravariant with respect to T if their constructors accepted one or more consumers of T. Iโ€™m not sure that there are many advantages of the class over the interface, except for the ability to use the โ€œnewโ€ one rather than using call the static factory method (most likely from a class whose name is similar to the interface), but I can, of course, see cases of use to support immutable covariance structures.

+1
Aug 24 '11 at 18:05
source share



All Articles