在以下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
调用res0.foo
是一个反思性调用,因为res0
已向Object
添加了新的结构成员。但是,调用res2.foo(1, 2)
并不会抱怨缺少import language.reflectiveCalls
。显然def foo: String
导致了这种沉默。 res2.foo(1, 2)
不应该反思吗?有人可以解释编译器保持安静的原因吗?