Java 10 是否提供 val 关键字?如果没有,为什么不呢? [英] Does Java 10 provide a val keyword? If not, why not?

查看:27
本文介绍了Java 10 是否提供 val 关键字?如果没有,为什么不呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 10 为 var 关键字="noreferrer">本地类型推断.

Java 10 brings a C#-like var keyword for local type-inference.

但是 Java 10 是否也提供了 val 关键字,就像 在 Scala 中找到?

But does Java 10 also provide a val keyword, as is found in Scala?

val 会像 var 一样工作,但绑定是 final.

val would work like var but the binding would be final.

var x = "Hello, world. ";
x = "abc"; // allowed

val y = "Hello, world. ";
y = "abc"; // forbidden

如果没有,是否有理由证明是这样?

If it does not, is there a reason that this is the case?

推荐答案

Java 10 中没有 val,如 JEP 286:局部变量类型推断:

There is no val in Java 10, as stated in JEP 286: Local-Variable Type Inference:

语法选择

对语法有多种意见.这里的两个主要自由度是使用什么关键字(var、auto 等),以及是否为不可变局部变量(val、let)提供单独的新形式.我们考虑了以下语法选项:

There was a diversity of opinions on syntax. The two main degrees of freedom here are what keywords to use (var, auto, etc), and whether to have a separate new form for immutable locals (val, let). We considered the following syntactic options:

  • var x = expr only (like C#)
  • var,加上 val 用于不可变的局部变量(如 Scala、Kotlin)
  • var,加上 let 用于不可变的局部变量(如 Swift)
  • auto x = expr(如 C++)
  • const x = expr(已经是一个保留字)
  • final x = expr(已经是一个保留字)
  • 让 x = expr
  • def x = expr(类似 Groovy)
  • x := expr(像 Go)

在收集大量输入后,var 明显优于 Groovy、C++ 或 Go 方法.对于不可变局部变量 (val, let) 的第二种句法形式存在大量意见分歧;这将是额外仪式以额外捕获设计意图的权衡.最后,我们选择只支持var.可以在此处.

After gathering substantial input, var was clearly preferred over the Groovy, C++, or Go approaches. There was a substantial diversity of opinion over a second syntactic form for immutable locals (val, let); this would be a tradeoff of additional ceremony for additional capture of design intent. In the end, we chose to support only var. Some details on the rationale can be found here.

这是主要原因:

我知道这是人们真正关心的部分 :) 经过考虑长篇大论的利弊,似乎有一个明显的赢家——var-only.原因包括:

I know this is the part people really care about :) After considering the pros and cons at length, there appears to be an obvious winner -- var-only. Reasons for this include:

  • 虽然它不是调查中最受欢迎的选择,但它是显然,大多数人都同意的选择.许多人讨厌变量/值;其他人讨厌 var/let.几乎没有人讨厌 var-only.

  • While it was not the most popular choice in the survey, it was clearly the choice that the most people were OK with. Many hated var/val; others hated var/let. Almost no one hated var-only.

C# 的经验——它只有 var——表明这是一个类 Java 语言的合理解决方案.没有风潮在 C# 中对 "val" 的需求.

Experience with C# -- which has var only -- has shown that this is a reasonable solution in Java-like languages. There is no groundswell of demand for "val" in C#.

减少不变性仪式的愿望当然是很好,但在这种情况下是推错了杠杆的一端.我们在不变性方面需要帮助的地方是字段,而不是本地人.但var/val 不适用于字段,而且几乎肯定永远不会.

The desire to reduce the ceremony of immutability is certainly well-taken, but in this case is pushing on the wrong end of the lever. Where we need help for immutability is with fields, not with locals. But var/val doesn't apply to fields, and it almost certainly never will.

如果获得可变性控制的增量开销类型推断为零,可能有更强的情况,但它是很明显,许多人发现两个不同的主要关键字是分心使他们的眼睛无法快速集中在重要的事情上东西.如果变量名比类型更重要,那么它们更重要比可变性修饰符也重要.

If the incremental overhead of getting mutability control over that of type inference were zero, there might be a stronger case, but it was clear that many people found two different leading keywords to be a distraction that kept their eyes from quickly settling on the important stuff. If variable names are more important than types, they're more important than mutability modifiers too.

(来源)

这篇关于Java 10 是否提供 val 关键字?如果没有,为什么不呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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