C#规范(团队?委员会?)曾经考虑过这种对象创建语法吗? [英] Has the C# spec (team? committee?) ever considered this object creation syntax?

查看:84
本文介绍了C#规范(团队?委员会?)曾经考虑过这种对象创建语法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前从未发布过这种性质的问题,因此,如果这不适用于SO,请不要伤害我的心情,然后将其删除.

为了使我关心的所有内容尽可能地靠近左边缘,我一直希望我可以写类似的东西:

In the interest of keeping everything I care about as close to the left margin as possible, I keep wishing I could write something like:

DataService1.DataEntities dataEntities = new(constructorArg1, ...)

我认为另一个原因是,我喜欢在赋值的右侧已经存在该类型时使用var获得的额外屏幕空间,但是我的大脑在寻找左侧的类型上已经花了很多年了.再说一次,卡住我的举动并不是一个希望获得规格的好理由...

I think another reason is I like the extra screen real estate I get by using var when the type is already present on the right side of the assignment, but my brain has too many years of looking for the type on the left side. Then again, being stuck in my ways isn't such a good reason to wish for a spec...

推荐答案

C#设计委员会曾经考虑过这种对象创建语法吗?

Has the C# design committee ever considered this object creation syntax?

是的,我们有.我们几年前考虑过.作为此主张的证据,请参见此处的文章的最后一段:

Yes, we have. We considered it a couple years ago. As evidence of this claim, see the last paragraph of my article here:

http://blogs.msdn .com/b/ericlippert/archive/2009/01/26/why-no-var-on-fields.aspx

设计团队的共识是,这是一个不错的功能",但不足以使它值得设计,实施,测试,文档化和维护该功能的相当大的成本.

The consensus of the design team was that this was a "nice to have" feature but not sufficiently compelling that it was worth the considerable cost of designing, implementing, testing, documenting and maintaining the feature.

我还注意到,我链接到的博客条目的评论对该功能非常负面;似乎很多人发现该语法没有吸引力.这也不利于该功能的实现.

I note also that the comments to the blog entry I linked to are very negative about the feature; it seemed like a lot of people found the syntax unattractive. That was also points against doing the feature.

但是,如果您可以将所建议的语法与其他语言功能结合使用,以促进简洁类型的不可变类型的声明,那么它会变得特别好.如果我们在该语言的假设未来版本中提供了这样的功能,则您提出的语法将变得更加引人注目.

However, the proposed syntax becomes particularly nice if you can combine it with other language features that promote the concise declaration of immutable types; if we do such a feature in a hypothetical future version of the language, then the syntax you propose becomes more compelling.

我进一步指出,我们通常会拒绝要求从外部"到内部"进行推理的功能;我们更喜欢类型信息从内而外的流动.例如考虑以下问题:

I note further that we in general resist features that require inference from "outside" to "inside"; we prefer that type information flow from the inside out. Consider for example this problem:

M(new(blah));

假设M有两个重载,一个重载C,一个重载D.是"new C(blah)"还是"new D(blah)"?可能是.现在我们必须分析两者!如果它们都起作用,那么我们必须找出哪个更好.

Suppose M has two overloads, one that takes a C, and one that takes a D. Is that "new C(blah)" or "new D(blah)"? It could be either. Now we have to analyze both! And if they both work then we have to figure out which is better.

情况变得更糟.假设你有

It gets worse. Suppose you have

M(new(new(blah)));

其中M再次带有C和D,C带有两个构造函数,分别带有E或F,D带有两个构造函数,分别带有G和H.

where again M takes a C and a D, and C has two constructors that take an E or an F, and D has two constructors that take an G and an H. Which of:

M(new C(new E(blah)));
M(new C(new F(blah)));
M(new D(new G(blah)));
M(new D(new H(blah)));

选择

,为什么?

is chosen, and why?

当您从外到内推理时,您很快就会陷入组合爆炸",其中要分析的案例数在嵌套深度为O(c n ).

When you reason from outside to inside you quickly get into "combinatoric explosions" where the number of cases to analyze becomes O(cn) in the depth of the nesting.

C#确实以这种方式对lambda进行推理,相信我,这是使编译器变得高效和正确的最难的部分之一.我们不希望向构造函数添加类似的功能.如果要添加此语法,则可能仅限于通过分析变量声明或赋值表达式的左侧明确知道类型的场景.

C# does reason in this manner for lambdas and that is one of the hardest parts of the compiler to make performant and correct, believe me. We're not eager to add a similar feature to constructors. If we were to add this syntax it would probably be limited to scenarios in which the type was unambiguously known by analyzing the left hand side of a variable declaration or assignment expression.

(我一如既往地注意到,埃里克(Eric)关于没有时间表或预算的未经宣布且完全虚构的产品中假设的未来语言功能的想法仅是出于娱乐目的,而不应被解释为对任何特定未来产品的承诺.任何特定功能集.)

(As always, I note that Eric's musings about hypothetical future language features in unannounced and entirely fictional products that do not have schedules or budgets is for entertainment purposes only, and not to be construed as a promise of any particular future product with any particular feature set.)

这篇关于C#规范(团队?委员会?)曾经考虑过这种对象创建语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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