C#7值元组/解构不对称 [英] C#7 value tuple/deconstruction asymmetry

查看:88
本文介绍了C#7值元组/解构不对称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里拨弄.

给定一个函数(字符串a,字符串b)F(),您可以解构它返回的元组:

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

var (a, b) = F();

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

或者您也可以分配它:

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

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

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

类解构函数的行为类似于第一种情况.给定带有 Deconstructor(出字符串a,出字符串b)的类 C :

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);
}

尽管在解构情况和赋值情况之间的语法上存在视觉相似性,但为元组分配引用与将类分解为变量然后将其放入新的元组并不相同.但是,您可以将(int x,int y)隐式转换为(double x,double y).值元组是一种语法糖功能,它在功能上看起来像它在做的事情一样,无需理会实现细节.

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.

如果我想到了这一点,C#团队就会想到这一点,并且如果他们选择不对隐式转换添加魔术"支持,那么他们就有充分的理由 1 .

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.

是否有积极的理由为什么自动进行隐式转换会是个坏主意?

还是仅仅是被认为不足以证明成本合理的那些功能之一?

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

这是来自那把小提琴的代码:

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 C#团队可能并不比每个人都聪明,但是我从来没有输过赌注,因为他们至少和我一样聪明.

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.

推荐答案

在C#7发布之前,要求进行解构的功能就像隐式转换器一样(这对我来说很偏向我,因此我对此有偏见).团队的反馈是(正如我所阅读的那样),因为它被要求离C#7版本太近,并且实施时间太长,因此没有考虑.由于这将是一个重大突破,所以这将永远不会发生.

The ability to have deconstructs act like implicit converters was something that was asked for (by me, so I'm biased here) before C# 7 was released. The response from the team was (as I read it anyway) that it was asked for too near to the C# 7 release and would have taken too long to implement, so wasn't up for consideration. As it would now be a breaking change, it's not something that will ever happen.

请参阅"允许解构和隐式运算符同时支持解构和转换为元组类型"roslyn repo问题,用于对此问题的实际讨论.

Please see the "Allow Deconstruct and implicit operator to both support deconstruction and conversion to tuple types" roslyn repo issue for the actual discussion on the matter.

这篇关于C#7值元组/解构不对称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆