Open RikkiGibson opened 2 years ago
My thinking here is that the nullability of an argument for a 'MaybeNullWhen' parameter shouldn't contribute its nullability to type inference.
LDM had decided that nullability attributes should not affect method type inference.
In the example provided above, would the following annotation be more suitable instead?
public static bool TryGetFoo<T>(this IFoo<T> a, [NotNullWhen(true)] out T b)
would the following annotation be more suitable instead?
public static bool TryGetFoo<T>(this IFoo<T> a, [NotNullWhen(true)] out T b)
I do not think so. This feels similar to the dictionary scenario, where a lookup failure means the output may be null even if T
disallows null. A successful lookup is non-null depending on whether T
allows null. If the standard Dictionary
, IReadOnlyDictionary
, etc. interfaces happened to be variant, we would have usability problems like what you see in this issue.
If you could share any relevant LDM notes, as well as any scenarios which were problematic when factoring attributes into type inference, that would help here.
It feels like the value which a method will assign into an out
parameter never depends on whether the out
parameter happens to be annotated as nullable. I wonder if there's some change to the rules we could make which reflects that and solves this scenario. Obviously we still have to do something reasonable for the void M<T>(out T value);
scenario, though.
Discussed in https://github.com/dotnet/roslyn/discussions/60519
My thinking here is that the nullability of an argument for a 'MaybeNullWhen' parameter shouldn't contribute its nullability to type inference. If we ensure that, then we should get the desired behavior that the user has described here.