dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

Nullable annotations for SpecializedTasks.Default #38260

Open sharwell opened 5 years ago

sharwell commented 5 years ago

Currently we don't have a way to document the semantics of SpecializedTasks.Default<T> such that the compiler knows that the result is nullable for reference types. This issue is requesting support from the language team to understand the best pattern(s) to employ in cases like this.

https://github.com/dotnet/roslyn/blob/98b82ec70a2d9e6953a588464bce44fa245182b3/src/Workspaces/Core/Portable/Utilities/SpecializedTasks.cs#L20-L23

jasonmalinowski commented 5 years ago

Chatting with @agocke about a similar issue earlier today (not this one though), it seems like the opinion might have been to just write SpecializedTasks.Default<string?>().

sharwell commented 5 years ago

@jasonmalinowski If we could write the following, then your approach would be fairly ideal:

public static Task<T> Default<T>()
  where T : defaultable
{
  // ...
}