为什么 Scala 选择在变量名后面加上类型?
在 Scala 中,变量的声明方式如下:
var stockPrice: Double = 100.
其中类型 (Double) 跟随标识符 (stockPrice)。传统上,在 C、Java、C# 等命令式语言中,类型名称位于标识符之前。
double stock_price = 100.0;
这纯粹是一个品味问题,还是最后的类型名称对编译器有任何帮助? Go也有同样的风格。
In Scala variables are declared like:
var stockPrice: Double = 100.
Where the type (Double) follows the identifier (stockPrice). Traditionally in imperative languages such as C, Java, C#, the type name precedes the identifier.
double stock_price = 100.0;
Is it purely a matter of taste, or does having the type name in the end help the compiler in any way? Go also has the same style.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
凯文说得对。主要观察结果是,只要类型是简短的关键字(例如 int 或 float),“类型名称”语法就可以很好地工作:
以一个的价格,您可以得到两块信息:“新定义从这里开始”和“这是定义的(结果)类型”。但现在我们已经远远超出了简单原始类型的范围。如果您编写的
定义最重要的部分(名称)被隐藏在类型表达式后面。比较
它说得很清楚
Kevin's got it right. The main observation is that the "type name" syntax works great as long as types are short keywords such as int or float:
For the price of one you get two pieces of information: "A new definition starts here", and "here's the (result) type of the definition". But we are way past the area of simple primitive types nowadays. If you write
the most important part of what you define (the name) is buried behind the type expression. Compare with
It says clearly
除了支持类型推断之外,这还具有人体工程学优势。
对于任何给定的变量名称+类型,名称很可能是更重要的信息。将其移至左侧使其更加突出,并且一旦您习惯了这种样式,代码就更具可读性。
其他符合人体工程学的优点:
val
、var
或def
,而不是其类型,它们都整齐地排列在一列中。*
是后面名称的前缀,而不是 C/C++ 中前面类型的后缀,或[] 是 Java 中名称和类型的有效后缀)仍然会让新手或语言转换者感到困惑,并且在单行上声明多个变量时会导致一些非常实际的错误。 Scala 在这里没有留下任何怀疑和混乱的余地。
As well as supporting type inference, this has an ergonomic benefit too.
For any given variable name + type, chances are that the name is the more important piece of information. Moving it to the left makes it more prominent, and the code more readable once you're accustomed to the style.
Other ergonomic benefits:
val
,var
ordef
before member names, instead of their type, they all neatly line up in a column.*
being a prefix on the following name and not a suffix on the preceeding type in C/C++, or[]
being a valid suffix on both names and types in Java) are still confusing to newcomers or language converts, and cause some very real errors when declaring multiple variables on a single line. Scala leaves no room for doubt and confusion here.它是后来的,以便可以将其删除以进行类型推断:
但是,命令式语言传统上首先具有类型,这是事实。例如,帕斯卡就没有。
现在,C 就是这样做的,而 C++、Java 和 C# 都是基于 C 的语法,所以它们自然也是这样做的,但这与命令式语言完全无关。
It's afterwards so that it can be removed for type inference:
However, it is not true that imperative languages traditionally have types first. For example, Pascal doesn't.
Now, C does it, and C++, Java and C# are based on C's syntax, so naturally they do it that way too, but that has absolutely nothing to do with imperative languages.
应该注意的是,即使是 C 也不会“传统地”在变量名之前定义类型,但确实允许声明交错。
其中 foo 的类型在词法上在其之前和之后声明。
除此之外,我猜这是一个没有区别的区别。编译器开发人员当然不会关心这样或那样。
It should be noted that even C doesn't "traditionally" define the type before the variable name, but indeed allows the declarations to be interleaved.
where the type for foo is declared both before and after it, lexically.
Beyond that, I'm guessing this is a distinction without a difference. The compiler developers certainly couldn't care one way or another.