Allowing sealed classes as generic constraints to help annotate nullability relationships #3013
Unanswered
safakgur
asked this question in
Language Ideas
Replies: 2 comments
-
It's definitely an interesting problem, and the suggested solution does have a lot going for it. However I'm not sure this would work in most cases. For example: Foo<T> Bar<T>(Foo<T> foo)
where T : string?
{
if (foo.Value is null) { return new Foo<T?>(null); }
else { return new Foo<T>(foo.Value.Substring(1,2)); }
} This will fail to compile, since the return type of |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think this will get help if |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the following example, there is no way to annotate the nullability of the returning Foo's generic argument to be the same as the nullability of the accepted Foo's generic argument.
A signature like
Foo<T> Bar(Foo<T> foo)
would allow us to preserve the nullability and adding a type constraint likewhere T : SomeClass?
would allow us to use type-specific functionality in the method. But sealed classes like string can't be used as type constraints.I understand it didn't make sense before but I think it may be useful to lift this restriction (it is already allowed in IL) now with nullable reference types in the wild, so we can do:
I also asked about this on Stack Overflow.
Beta Was this translation helpful? Give feedback.
All reactions