2

在以下 REPL 会话中:

scala> new Object { def foo = "bar" }
res0: Object{def foo: String} = $anon$1@131a24c

scala> res0.foo
<console>:9: warning: reflective access of structural type member method foo should be enabled by making the implicit value language.reflectiveCalls visible.
          res0.foo
               ^
res1: String = bar

scala> trait Foo { def foo: String }
defined trait Foo

scala> new Foo { def foo = "bar"; def foo(bar: Int, baz: Int) = "bar" }
res2: Foo{def foo(bar: Int,baz: Int): String} = $anon$1@18a4aef

scala> res2.foo(1, 2)
res4: String = bar

调用 tores0.foo是一个反射调用,因为res0已向 . 添加了一个新的结构成员Object。但是,调用 tores2.foo(1, 2)并没有抱怨缺少import language.reflectiveCalls. 显然def foo: String是造成了那种沉默。不res2.foo(1, 2)应该是反射电话吗?有人可以解释编译器保持安静的原因吗?

4

0 回答 0