Roslyn: Test plan for allowing `default` literal in deconstruction

Created on 17 Mar 2018  路  5Comments  路  Source: dotnet/roslyn

(int i, string j) = default; // error CS8131: Deconstruct assignment requires an expression with a type on the right-hand-side.
(int i, string j) = (default, default); // OK

This is probably a minor language change (ie. needs to be confirmed with LDM), but I think it would make sense to allow (and give default the type from the left-hand-side). This would be consistent with default in tuple equality ((1, 2) == default).

From discussion in LDM in September, this should be handled more generally as the RHS being target-typed. This means we can use a switch expression with typeless tuple literals (1, null) and they should get target-typed.

Note: some IDE scenarios may need to be updated as a result (see UseDeconstructionTests.cs after https://github.com/dotnet/roslyn/pull/25282/files and https://github.com/dotnet/roslyn/pull/26140/files are merged)

Specification

  • [ ] Specification checked in to csharplang.
2 - Ready Area-Compilers Feature Request Test Test-Gap

All 5 comments

Why would permitting this be better than requiring that the user write
c# int i = 0; string j = null;

One answer to my own question is that the left-hand-side might consist of existing variables, not declarations of new ones:

(x, y) = default

which can be done in an expression, but cannot be broken in two in an expression

c# class Point { public readonly int X, Y; public Point(int x, int y) => (X, Y) = (x, y); public Point() => (X, Y) = default; }

also, x=y=default works as long as all vars have the same type. plus, it's useful to init out params in one go. (p1,p2)=default;

The feature is in feature/decon-default branch, and it is ready to merge as we decide the release branch.

Was this page helpful?
0 / 5 - 0 ratings