[Proposal] Allow covariance (out) for generic out parameters #5623
Replies: 4 comments 3 replies
-
IIRC this is because |
Beta Was this translation helpful? Give feedback.
-
Hopefully this will be addressed nonetheless, as I just hit this limitation. |
Beta Was this translation helpful? Give feedback.
-
Allowing this with the current behaviour of The following wrong example uses using System.Runtime.CompilerServices;
VarianceExample<Derived> f = (ref Base x, out Derived y) => {
y = new Derived();
x = new Base();
Derived readBack = y;
Console.WriteLine($"readBack = {readBack}");
};
VarianceExample<Base> v = Unsafe.As<VarianceExample<Base>>(f);
Base b = new();
v(ref b, out b);
delegate void VarianceExample<T>(ref Base x, out T y);
class Base { }
class Derived : Base { } Do not use |
Beta Was this translation helpful? Give feedback.
-
Addressing this can potentially unlock covariance in many important interfaces, e.g. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi. I tried searching for this but didn't find it. Feel free to close if it's been discussed and dismissed earlier.
I am suggesting to allow covariance
out
for generic out parameters.Today the C# compiler responds:
From my uninformed point of view an
out
parameter is similar to return values and this does compile and work as expected in C#.There might be subtle or obvious details that I am not seeing that makes covariance on
out
parameters not possible.Motivation:
The reason I ran into this was that I been experimenting with parser combinators in C# and ended up with this definition:
I wanted this to be covariant but as the examples above demonstrate currently this is not supported.
Beta Was this translation helpful? Give feedback.
All reactions