(另见,Why are `private val` and `private final val` different?,非常接近,但不一样)
解决方法
它可能意味着“不能在子类中覆盖”,因此最终变量.
apm@mara:~$skala -Yoverride-vars Welcome to Scala version 2.11.0-20130811-132927-95a4d6e987 (OpenJDK 64-Bit Server VM,Java 1.7.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> trait Foo { var v = 7 } defined trait Foo scala> trait Bar extends Foo { override var v = 8 } defined trait Bar scala> trait Foo { final var v = 7 } defined trait Foo scala> trait Bar extends Foo { override var v = 8 } <console>:8: error: overriding variable v in class Foo$class of type Int; variable v cannot override final member trait Bar extends Foo { override var v = 8 } ^
final val i = 7是一个常量值定义(也就是编译时间常数),但是val i = 7不是,不管访问修饰符如何.
之前引用过这个,但是5.2规范:
The final modifier applies to class member deFinitions and to class
defini- tions. A final class member deFinition may not be overridden
in subclasses. A final class may not be inherited by a template. final
is redundant for ob- ject deFinitions. Members of final classes or
objects are implicitly also final,so the final modifier is generally
redundant for them,too. Note,however,that constant value
deFinitions (§4.1) do require an explicit final modifier,even if they
are defined in a final class or object. final may not be applied to
incom- plete members,and it may not be combined in one modifier list
with sealed.
和4.1
A constant value deFinition is of the form
final val x = e
where e is a constant expression (§6.24). The final modifier must be
present and no type annotation may be given. References to the
constant value x are themselves treated as constant expressions; in
the generated code they are replaced by the def- inition’s right-hand
side e.
编辑:抱歉,没有注意到你特意没有询问.孩子们正准备睡觉,刷牙,所有这一切,有点分心.