什么是“无全局类型推断"?关于Scala是什么意思? [英] What does "no global type inference" mean regarding Scala?

查看:156
本文介绍了什么是“无全局类型推断"?关于Scala是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过Scala的类型推断不是全局的,因此人们必须在方法上放置类型标注. (这将是本地"类型推断吗?)

I have read that Scala's type inference is not global so that is why people must place type annotations on the methods. (Would this be "local" type inference?)

我只有一点点了解,原因是由于其面向对象的性质,但我却不清楚.是否有全局类型推断"的解释?为什么Scala无法拥有初学者可能理解的解释?

I only a little understand that the reason is from its object-oriented nature, but clarity eludes me. Is there an explanation for "global type inference" and why Scala cannot have it that a beginner might understand?

推荐答案

全局类型推断的典型示例是

The typical example for a global type inference is Hindley-Milner: It takes a given program and "calculates" all the necessary types. However in order to achieve this, the given language needs to have some properties (there are extensions to HM, which try to overcome some of these restrictions). Two things HM doesn't like are inheritance and method overloading. As far as I understand these are the main obstacles for Scala to adopt HM or some variant of it. Note that in practice even languages which heavily rely on HM never reach a 100% inference, e.g. even in Haskell you need a type annotation from time to time.

因此,Scala使用类型限制更有限的(如您所说的本地")类型推理,这总比没有好.据我所知,Scala团队在可能的情况下尝试改进各个发行版之间的类型推断,但到目前为止,我只看到了较小的步骤.与HM样式类型推断器之间的差距仍然很大,无法完全消除.

So Scala uses a more limited (as you say "local") form of type inference, which is still better than nothing. As far as I can tell the Scala team tries to improve the type inference from release to release when it is possible, but so far I've seen only smaller steps. The gap to a HM style type inferencer is still huge, and can't be closed completely.

这篇关于什么是“无全局类型推断"?关于Scala是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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