15

I have a vector class with two deconstruction methods as follows:

public readonly struct Vector2
{
    public readonly double X, Y;

    ...

    public void Deconstruct( out double x, out double y )
    {
        x = this.X;
        y = this.Y;
    }

    public void Deconstruct( out Vector2 unitVector, out double length )
    {
        length = this.Length;
        unitVector = this / length;
    }
}

Somewhere else I have:

Vector2 foo = ...
(Vector2 dir, double len) = foo;

This gives me:

CS0121: The call is ambiguous between the following methods or properties: 'Vector2.Deconstruct(out double, out double)' and 'Vector2.Deconstruct(out Vector2, out double)'

How is this ambiguous?

Edit: Calling Deconstruct manually works fine:

foo.Deconstruct( out Vector2 dir, out double len );
Chris
  • 5,442
  • 17
  • 30

1 Answers1

17

This is by design in C#. Overloads of Deconstruct must have different arity (number of parameters), otherwise they are ambiguous.

Pattern-matching does not have a left-hand-side. More elaborate pattern-matching scheme is to have a parenthesized list of patterns to match, and we use the number of patterns to decide which Deconstruct to use. - Neal Gafter https://github.com/dotnet/csharplang/issues/1998#issuecomment-438472660

Asik
  • 21,506
  • 6
  • 72
  • 131
  • @Chris Yeah, looks like they decided to not use the type declarations for overload resolution in part so the type declarations can use `var`. – Nat Apr 20 '19 at 06:12