-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tuples deconstructing support #1832
Comments
There's two fundamentally different language constructs here:
var (a, b) = expr1;
var ((c, d), (e, f)) = expr2; These somewhat overlap with pattern matching (#2048), as patterns can also contain this style of deconstruction.
(Console.CursorLeft, Console.CursorTop) = expr1;
((Get(0).Prop, Get(1).Prop), (Get(2).Prop, Get(2).Prop)) = expr2; These are a bit more tricky:
|
Though I guess there isn't really a difference between
So in some sense the expression form is the more general one; it's just that the copying of the outputs can be optimized out if it's just a copy between local variables. |
ILAst representation of deconstructionRequirements:
Idea: class DeconstructInstruction {
InstructionCollection<StLoc> lhsTargetInit; // these slots allow inlining
RecursiveMatch deconstruct; // TestedOperand is the RHS; the pattern determines the nesting structure
Block conversions; // block with `stloc new_temp = implicit_conversion(temp_from_deconstruct)`
Block assignments; // block with the actual assignments `call set_Prop(ldloc lhs_target_var, ldloc new_temp)`
} Full example: struct CustomString
{
public static implicit operator string(CustomString s) => null;
}
class C {
public string Prop { get; set; }
public C Get(int i) => null;
public void Deconstruct(out string a1, out CustomString a2)
{
a1 = "a";
a2 = new CustomString();
}
public (C, C) GetTuple() => throw null;
public void Test()
{
((Get(0).Prop, Get(1).Prop), (Get(2).Prop, Get(2).Prop)) = GetTuple();
}
} The deconstruction in
Invariants:
|
Deconstruction TODO:
|
ILSpy version 6.0.0.5410-alpha1
Continuing games with new features, mainly tuples deconstructing
https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct
input test code
ILSpy:
The text was updated successfully, but these errors were encountered: