-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow type parameters to be declared co/contra variant on classes. #171
Comments
How would this work with accessibility? For example, could If that were allowed, how would you make sure the following couldn't happen? class Task<out T>
{
private T value;
public static void SetValue(Task<T> task, T value)
{
task.value = value;
}
}
…
Task<Pet>.SetValue(new Task<Dog>(), new Cat()); If it wasn't allowed, then I think that significantly limits the usefulness of the feature. For example, I don't know how would you implement |
Presumably it would be legal to declare private fields of co/contra variant generics. The static method you described could not be declared as |
I'm not sure it would be feasible... there were good reasons to limit variance to interfaces and delegates in C# 4 (although I admit I don't remember exactly what they were). I agree that the |
Yeah if TPL used an |
@mburbea see this answer by Eric Lippert: http://stackoverflow.com/a/2734070/98713 |
@svick and @mburbea , so if I understand correct, Task can be implemented with something like:
So we split Task and result storage into to classes: immutable covarint Task and non-variant result storage. |
This would be a large undertaking including required CLR support, for marginal gain in language expressiveness. We don't think this is where we want to invest. |
Currently type parameters can be declared co/contra variant on either an interface or a delegate. I propose adding support for classes.
The rules would remain the same, any
in T
cannot appear as any method return type nor on gettable properties. Anyout T
cannot appear as a parameter or on settable properties. My main motivation for this is really around things likeTask<T>
where I sometimes want to collectTask<Dog>
,Task<Cat>
,Task<Fish>
into a collection ofIEnumerable<Task<Pet>>
and whenAlll/whenAny and only inspect their pet based result.It cannot be legal on structs as the assignment would be a copy operation.
The text was updated successfully, but these errors were encountered: