Should deconstruction be able to automatically invoke the implicit cast operator? #7914
Answered
by
HaloFour
suugbut
asked this question in
Language Ideas
-
Should deconstruction be able to automatically invoke the implicit cast operator? //var (x, y) = new Complex(1, 0); // elegant but it causes compile time error.
var (x, y) = ((float, float))new Complex(1, 0); // a bit verbose
class Complex(float re, float im)
{
public float Re { get; set; } = re;
public float Im { get; set; } = im;
public static implicit operator (float re, float im)(Complex c) => (c.Re, c.Im);
} |
Beta Was this translation helpful? Give feedback.
Answered by
HaloFour
Feb 5, 2024
Replies: 1 comment
-
Add a custom deconstruct method: class Complex(float re, float im)
{
public float Re { get; set; } = re;
public float Im { get; set; } = im;
public void Deconstruct(out float re, out float im) => (re, im) = (Re, Im);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
suugbut
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add a custom deconstruct method: