Java 10 是否提供 val 关键字?如果不是,为什么不呢?
Does Java 10 provide a val keyword? If not, why not?
Java 10 为 local type-inference 带来了类似 C# 的 var
关键字。
但是 Java 10 是否也像 found in Scala 一样提供了一个 val
关键字?
val
将像 var
一样工作,但绑定将是 final
。
var x = "Hello, world. ";
x = "abc"; // allowed
val y = "Hello, world. ";
y = "abc"; // forbidden
如果不是,是否存在这种情况的原因?
Java10中没有val
,如JEP 286: Local-Variable Type Inference中所述:
Syntax Choices
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, plus val for immutable locals (like Scala, Kotlin)
- var, plus let for immutable locals (like Swift)
- auto x = expr (like C++)
- const x = expr (already a reserved word)
- final x = expr (already a reserved word)
- let x = expr
- def x = expr (like Groovy)
- x := expr (like Go)
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.
这是主要的推理:
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:
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.
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#.
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.
(Source)
因为 Java 中有 final var
。如果我们也有 val
,就会有两个意思相同的东西。这个不好。应该只有一种方式来表达特定的东西。
如果你想使用"val",即"final var",你总是可以使用Lombok's val。
Java 10 为 local type-inference 带来了类似 C# 的 var
关键字。
但是 Java 10 是否也像 found in Scala 一样提供了一个 val
关键字?
val
将像 var
一样工作,但绑定将是 final
。
var x = "Hello, world. ";
x = "abc"; // allowed
val y = "Hello, world. ";
y = "abc"; // forbidden
如果不是,是否存在这种情况的原因?
Java10中没有val
,如JEP 286: Local-Variable Type Inference中所述:
Syntax Choices
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, plus val for immutable locals (like Scala, Kotlin)
- var, plus let for immutable locals (like Swift)
- auto x = expr (like C++)
- const x = expr (already a reserved word)
- final x = expr (already a reserved word)
- let x = expr
- def x = expr (like Groovy)
- x := expr (like Go)
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.
这是主要的推理:
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:
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.
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#.
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.
(Source)
因为 Java 中有 final var
。如果我们也有 val
,就会有两个意思相同的东西。这个不好。应该只有一种方式来表达特定的东西。
如果你想使用"val",即"final var",你总是可以使用Lombok's val。