隐式输入; 为什么只是局部变量?
有谁知道或关心推测为什么隐式类型仅限于局部变量?
var thingy = new Foo();
但为什么不...
var getFoo() {
return new Foo();
}
Does anyone know or care to speculate why implicit typing is limited to local variables?
var thingy = new Foo();
But why not...
var getFoo() {
return new Foo();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
埃里克·利珀特(Eric Lippert)就这个主题写了一篇完整的博客文章。
总之,主要问题是需要对 C# 编译器进行重大重新架构才能实现这一点。 目前,声明以单次传递的方式进行处理。 由于能够在推断变量之间形成循环,因此这将需要多次传递。 VB.NET 也有大致相同的问题。
Eric Lippert did an entire blog post on the subject.
In summary, the main problem is that it would have required a major re-architecture of the C# compiler to do so. Declarations are currently processed in a single pass manner. This would require multiple passes because of the ability to form cycles between inferred variables. VB.NET has roughly the same problem.
贾里德的回答中有一个很棒的链接,指向一个很棒的主题。
我认为它没有明确回答这个问题。
为什么不呢?
这样做的原因是:
如果?
你可以推断
GetFoo
将返回Foo
,但是您必须跟踪该方法及其子级进行的所有调用,以推断类型。 就目前情况而言,C# 编译器并非设计为以这种方式工作。 在推断类型的代码可以运行之前,它需要在此过程的早期阶段使用方法和字段类型。在纯粹的审美层面上,我发现方法上的 var 定义令人困惑。 我认为显式总是有帮助的地方,它可以防止您意外返回导致您的签名和大量其他依赖方法签名发生更改的类型而搬起石头砸自己的脚。 最糟糕的是,如果您返回一个返回对象的方法的值并且碰巧很幸运,您可能会更改方法链的所有签名,甚至不知道自己这样做了。
我认为 var 方法最好留给像 Ruby 这样的动态语言
Jared has a fantastic link in his answer, to a fantastic topic.
I think it does not answer the question explicitly.
Why not?
The reason for this is:
What if?
You could deduce that
GetFoo
is going to returnFoo
, but you will have to trace through all the calls that method makes and its children makes just to infer the type. As it stands the C# compiler is not designed to work in this way. It needs method and field types early in the process before the code that infers types can run.On a purely aesthetic level I find the var definitions on methods confuse things. Its one place where I think being explicit always helps, it protects you from shooting your self in the foot by accidentally returning a type that causes your signature and a ton of other dependent method signatures to change. Worst still, you could potentially change all you signatures of a method chain without even knowing you did so if you return the value of a method that returns object and happened to be lucky.
I think var methods are best left for dynamic languages like Ruby
因为它更容易做到。 如果你要推断所有类型,则需要像 Hindley Milner 类型推断系统这样的东西,它将把你喜爱的 C# 变成 Haskel 衍生语言。
Because it is much easyer to do. If you were to inference all types, one would need something like Hindley Milner type inference system which will in make your beloved C# into Haskel derivative language.
本质上,您遇到的问题是 C#(到目前为止)是一种静态类型语言。 定义为 var 的局部变量仍然是静态类型的,但在语法上是隐藏的。 另一方面,返回 var 的方法有很多含义。 它更像是一个使用接口,并且使用 var 不会获得任何好处。
Essentially, the issue you are running into is that C# (thus far) is a statically typed language. A local variable defined as var is still statically typed, but syntactically hidden. A method returning var, on the other hand, has many implications. It becomes more of an interface for usage, and you don't gain anything by using var.
您可以在 vs 2010 中使用动态
you can use in vs 2010 Dynamic
我认为这是因为该隐含类型的范围要宽得多,因此比单个方法的范围更容易导致问题。
I think it's because the scope of that implied type is much wider and therefore more likely to cause problems than within the scope of a single method.