csharplang icon indicating copy to clipboard operation
csharplang copied to clipboard

[Proposal]: `Task<T>` nullability covariance

Open 333fred opened this issue 11 months ago • 0 comments

Task<T> nullability covariance

  • Specification: None yet
  • Discussion: https://github.com/dotnet/csharplang/discussions/8996

Summary

Task<T> nullability covariance (LDM tentatively approved, needs design proposal for task-like types)

It should be possible to return a Task<string!> for a Task<string?>.

using System.Threading.Tasks;
#nullable enable
public class C 
{
    public Task<(bool Result, string? Extras)> M() 
    {
        return Task.FromResult((true, "")); // currently produces a warning
    }
}

Design meetings

Split issue from https://github.com/dotnet/csharplang/issues/3868 https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-28.md#nullability-improvements https://github.com/dotnet/csharplang/blob/main/meetings/2024/LDM-2024-09-06.md#taskt-nullability-covariance

333fred avatar Jan 06 '25 22:01 333fred