Cannot Have Nullable Return of Async Generic Method #4231
Replies: 2 comments 9 replies
-
Issue moved from microsoft/dotnet#1249
From @mysteryx93 on Saturday, September 19, 2020 5:51:30 PM Since there has been no response, here's my take on this. The .NET team could fix it in 2 ways.
On a different topic, a 3rd suggestion to help working with Generics
This works but has unnecessary boxing/unboxing, and disables type-checking The compiler should recognize this as valid IMO
|
Beta Was this translation helpful? Give feedback.
-
In C# 9, we've enabled unconstrained |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Issue moved from microsoft/dotnet#1249
From @mysteryx93 on Thursday, September 10, 2020 8:17:31 PM
With C# 8 Nullables, you can define a nullable generic like this
However, I'm running into a problem with a Generic Async method.
This would tell that Task is nullable, not T.
The only solution for now would be to add ! after default, but then the IDE will wrongly indicate to the users that the type is not null when it can be. This can work for private use (and can easily lead to errors), but not for a public API.
Is there any other solution?
Actually what I'd really need is a generic constraint "where T : nullable" because I need to document to pass in nullable types.
Currently, the only way of doing so is to have 2 copies, one with "where T : class" and one with "where T : struct", and using T? where ? is interpreted differently in each copy (Nullable vs nullable attribute). That can lead to doubling a lot of code.
Beta Was this translation helpful? Give feedback.
All reactions