为什么我不能使用数组初始化与隐式类型的变量? [英] Why can't I use the array initializer with an implicitly typed variable?

查看:169
本文介绍了为什么我不能使用数组初始化与隐式类型的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能使用数组初始化有一个隐式类型变量?

 的String []字= {苹果,草莓,葡萄};法律//
字符串[]字=​​新的String [] {苹果,草莓,葡萄};法律//
VAR字=新的[] {苹果,草莓,葡萄};法律//
VAR字=新的String [] {苹果,草莓,葡萄};法律//VAR字= {苹果,草莓,葡萄,桃花}; //非法

是否有技术原因的限制?为什么不能推断出它的类型像它会为:

  VAR数= 10;
VAR文本=你好;

编译器清楚地知道我想要做什么,它只是不会允许它:


  

CS0820:不能数组初始化分配到一个隐式类型的本地



更新:我编译使用四个法律数组声明方法的程序,它产生相同的IL: http://pastebin.com/28JDAFbL

这只是增加了我的困惑。而是这样的,因为规范是这么说的,是没有多大帮助。为什么是这样的规范呢?这里是什么道理呢?


解决方案

  

为什么我不能使用数组初始化与隐式类型的变量?为什么是这样的规范呢?这里是什么道理呢?


我是不是设计团队这个决定作出之时,和设计说明(*)的对这个问题保持沉默。但是,我问别人谁在房间里,2005年这个决定作出之时。

的解释是平淡无奇。设计团队一直都不是很满意摆在首位数组初始化语法。坦率地说这是彻头彻尾的离奇一个数组初始化并非当然pression和语法只能出现在本地或外地声明。它解析器复杂。看来奇怪的是,

  INT [] X = {1};

应该是合法的,但

  M({1});

不是

该数组初始化语法也使得在复杂的编辑时code分析过程中的错误恢复。假设你有这样的:

  C类
{
    无效M()
    {
        {
            INT结果=任何();
            ...
        }
        {
            INT结果= somethingElse();
            ...
        }
    }
}

和你开始在编辑器中键入一个新的声明:

 无效M()
    {
        INT [] X =
        {
            INT结果=任何();

和现在突然的解析器来处理歧义在不混淆不好的用户谁是即将输入方式的情况空;。显然,你不打算用code块来初始化局部变量,但是解析器是完全的权利范围内说,支架可以只的合法的是一个数组初始化的一部分在这里,因此,它是在INT结果即意想不到

所以,长话短说,经典数组的初始化对象是有点不好的特性的。由于向后兼容的原因,我们不能摆脱他们。但我们也不想让他们在的更多的地方,以鼓励其使用。

设计团队想出了prepending的理念新的[]到数组初始化,使的的纳入法制化的前pression,现在的问题是,解决了。没有经典的数组初始化不好的特性到语言的新领域蠕变,并有一个简洁但可读的语法,清楚地写着你正在一个新的数组这里。

这个故事的寓意是:设法得到它在第一时间,因为语法是永远


(*)我在寻找我也发现了一些有趣的事情:球队原本认为VAR可能不会被选择为特征的关键字;显然它的增长他们。此外,不只是被隐式类型的一个设计要求VAR本地人,也是一次init来本地人。很显然,我们从来没有实现初始化一次本地人。

Why can't I use the array initializer with an implicitly typed variable?

string[] words = { "apple", "strawberry", "grape" };                 // legal
string[] words = new string[]{ "apple", "strawberry", "grape" };     // legal
var words = new []{ "apple", "strawberry", "grape" };                // legal
var words = new string[]{ "apple", "strawberry", "grape" };          // legal

var words = { "apple", "strawberry", "grape", "peach" };             // ILLEGAL

Is there a technical reason for this limitation? Why can't it infer the type like it would for:

var number = 10;
var text = "Hello";

The compiler clearly knows what I am trying to do, it just won't allow it:

CS0820: Cannot assign array initializer to an implicitly typed local


Update: I compiled a program using the four legal array declaration methods, and it generates the same IL: http://pastebin.com/28JDAFbL

This just adds to my confusion. And "it is like this because the spec says so" is of little help. Why is the spec like this? What is the rationale here?

解决方案

Why can't I use the array initializer with an implicitly typed variable? Why is the spec like this? What is the rationale here?

I was not on the design team when this decision was made, and the design notes(*) are silent on this subject. However, I asked someone who was in the room in 2005 when this decision was made.

The explanation is prosaic. The design team was never very happy with the array initializer syntax in the first place. Frankly it is downright bizarre that an array initializer is not an expression and syntactically can only appear in a local or field declaration. It complicates the parser. It seems strange that

int[] x = {1};

should be legal, but

M({1});

is not.

The array initialization syntax also makes error recovery during code analysis at edit time complicated. Suppose you have something like:

class C
{
    void M()
    {
        {
            int result = whatever();
            ...
        }
        {
            int result = somethingElse();
            ...
        }
    }
}

and you start typing a new declaration in the editor:

    void M()
    {
        int[] x = 
        {
            int result = whatever();

and suddenly now the parser has to deal with disambiguating the situation in a way that does not confuse the poor user who is about to type "null;". Clearly you do not intend to initialize the local variable with the block of code, but the parser is perfectly within its rights to say that the brace can only legally be part of an array initializer here, and therefore it is the "int result" that is unexpected.

So, long story short, "classic" array initializers are a bit of a misfeature. We can't get rid of them because of backwards compatibility reasons. But we also don't want to encourage their use by allowing them in more places.

The design team came up with the idea of prepending "new[]" to the array initializer, and make that into a legal expression, and now the problem is solved. There is no "creep" of the classic array initializer misfeature into new areas of the language, and there is a terse but readable syntax that clearly says "you are making a new array here".

The moral of the story is: try to get it right the first time, because syntax is forever.


(*) In my search I did discover several interesting things: the team originally believed that "var" probably would not be the keyword chosen for the feature; apparently it grew on them. Also, one design called for "var" locals to not just be implicitly typed, but also be init-once locals. Obviously we never did implement init-once locals.

这篇关于为什么我不能使用数组初始化与隐式类型的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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