以下代码在 scala2 中按 (I) 预期工作,但在 scala3 中生成编译时错误。我试图在《Scala 编程》第五版和网络中找到解释,但失败了。有什么见解吗?
object Main {
def main(args:Array[String]): Unit = {
val foo = new Foo(7)
println(foo.double) // 14
val bar = new Foo(8) {
def triple:Int = this.i * 3
}
println(bar.double) // 16
println(bar.triple) // 24 in scala2; compile error in scala3
}
}
class Foo(val i:Int) {
def double:Int = i*2
}
编译错误:
-- [E008] Not Found Error: /tmp/example.scala:12:20 ----
12 | println(bar.triple)
| ^^^^^^^^^^
| value triple is not a member of Foo