Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#7 value tuple/deconstruction asymmetry

Fiddle here.

Given a function (string a, string b) F(), you can deconstruct the tuple it returns:

var (a, b) = F();

(string c, string d) = F();

Or you can just assign it:

var (a, b) e = F();

(string a, string b) f = F();

var g = F();  //  One of these things is not like the others.

Class deconstructors behave like the first case. Given a class C with Deconstructor(out string a, out string b):

var c = new C();

var (h, i) = c;

(string j, string k) = c;

But the compiler won't use the deconstructor to implicitly convert it to a tuple:

//  Cannot implicitly convert type 'C' to '(string a, string b)'
var (a, b) l = c;

Obviously you can mechanically write an implicit conversion based on the deconstructor:

public static implicit operator (string a, string b) (C c)
{
    c.Deconstruct(out string a, out string b);
    return (a, b);
}

Notwithstanding the visual similarity in the syntax between the deconstruction and assignment cases, assigning a reference to a tuple is not the same as deconstructing a class into variables and then putting them in a new tuple. However, you can implicitly convert (int x, int y) to (double x, double y). Value tuples are the kind of syntactic-sugar feature where it does what it looks like it does, and never mind the implementation details.

If I thought of this, the C# team thought of it, and if they chose not to add "magic" support for the implicit conversion, they had a good reason1.

Is there a positive reason why doing the implicit conversion automatically would have been a bad idea?

Or is it one of those features that just wasn't regarded as valuable enough to justify the cost?


Here's the code from that fiddle:

public class Program
{
    public static void Main()
    {
        (string a, string b) = F();

        (string a, string b) ab = F();

        Console.WriteLine($"a: {a} b: {b} ab: {ab}");


        var c = new C();

        (string d, string e) = c;

        //  Cannot implicitly convert type 'C' to '(string a, string b)'
        (string a, string b) f = c;

        Console.WriteLine($"d: {d} e: {e} f: {f}");

        //  Covariance
        (object c, object d) g = F();
        //  Implicit conversion
        (double x, double y) t = G();
    }

    public static (string a, string b) F() 
        => ("A", "B");

    public static (int x, int y) G() 
        => (0, 1);
}

public class C
{
    public String A = "A";
    public String B = "B";

    public void Deconstruct(out String a, out String b)
    {
        a = A;
        b = B;
    }
}

1 The C# team may not be smarter than everybody, but I've never lost money betting they were at least as smart as me.

like image 389
15ee8f99-57ff-4f92-890c-b56153 Avatar asked May 11 '17 16:05

15ee8f99-57ff-4f92-890c-b56153


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

Here's one way to achieve what you're trying to do: in your code sample, instead of (string a, string b) f = c;, use (string a, string b) f = (_, _) = c;.

You could also write a user-defined conversion from your type to the tuple type you need.

like image 63
Julien Couvreur Avatar answered Oct 09 '22 19:10

Julien Couvreur