为什么 Scala 选择在变量名后面加上类型?

发布于 2024-11-09 02:50:01 字数 265 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮光沉寂 2024-11-16 02:50:01

凯文说得对。主要观察结果是,只要类型是简短的关键字(例如 intfloat),“类型名称”语法就可以很好地工作:

int x = 1
float d = 0.0

以一个的价格,您可以得到两块信息:“新定义从这里开始”和“这是定义的(结果)类型”。但现在我们已经远远超出了简单原始类型的范围。如果您编写的

HashMap<Shape, Pair<String, String>> shapeInfo = makeInfo()

定义最重要的部分(名称)被隐藏在类型表达式后面。比较

val shapeInfo: HashMap[Shape, (String, String)] = makeInfo()

它说得很清楚

  • 我们在这里定义一个值,而不是一个变量或方法(val)
  • 我们定义的东西的名字是shapeInfo
  • 如果你关心它,这里是类型(HashMap[...])

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:

int x = 1
float d = 0.0

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

HashMap<Shape, Pair<String, String>> shapeInfo = makeInfo()

the most important part of what you define (the name) is buried behind the type expression. Compare with

val shapeInfo: HashMap[Shape, (String, String)] = makeInfo()

It says clearly

  • We define a value here, not a variable or method (val)
  • The name of the thing we define is shapeInfo
  • If you care about it, here's the type (HashMap[...])
夜司空 2024-11-16 02:50:01

除了支持类型推断之外,这还具有人体工程学优势。

对于任何给定的变量名称+类型,名称很可能是更重要的信息。将其移至左侧使其更加突出,并且一旦您习惯了这种样式,代码就更具可读性。

其他符合人体工程学的优点:

  • 在成员名称之前使用 valvardef,而不是其类型,它们都整齐地排列在一列中。
  • 如果您仅更改成员的类型,或者完全放弃它以支持推理,那么细粒度的 diff 工具将清楚地显示名称未更改
  • 中,在 val/var/def 之间进行更改非常清楚
  • 同样,在 diff推理 应该被视为 Scala 中的默认行为,您只需要在某些特定场景中进行类型规范,即使这样,它主要是为编译器完成的。因此,将它们放在声明的开头会强调错误的事情。
  • “名称:类型”而不是“类型名称”更符合大多数程序员实际考虑声明的方式,也更自然。
  • 指针和数组的 C/C++ 和 Java 约定不同(即 * 是后面名称的前缀,而不是 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:

  • With val, var or def before member names, instead of their type, they all neatly line up in a column.
  • If you change just the type of a member, or drop it entirely in favour of inference, then a fine-grained diff tool will clearly show that the name is unaltered
  • Likewise, changing between a val/var/def is very clear in diffs
  • inference should be considered default behaviour in Scala, you only need type specifications in certain specific scenarios, even then it's mostly done for the compiler. So putting them at the very start of a declaration emphasises the wrong thing.
  • "name: Type" instead of "Type name" more closely matches the way most programmers will actually think about a declaration, it's more natural.
  • The differing C/C++ and Java conventions for pointers and arrays (i.e * 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.
┊风居住的梦幻卍 2024-11-16 02:50:01

它是后来的,以便可以将其删除以进行类型推断:

var stockPrice: Double = 100.0
var stockPrice = 100.0

但是,命令式语言传统上首先具有类型,这是事实。例如,帕斯卡就没有。

现在,C 就是这样做的,而 C++、Java 和 C# 都是基于 C 的语法,所以它们自然也是这样做的,但这与命令式语言完全无关。

It's afterwards so that it can be removed for type inference:

var stockPrice: Double = 100.0
var stockPrice = 100.0

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.

ペ泪落弦音 2024-11-16 02:50:01

应该注意的是,即使是 C 也不会“传统地”在变量名之前定义类型,但确实允许声明交错。

int foo[];

其中 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.

int foo[];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文