What does “reflective access of structural type member method should be enabled…” warning mean in Scala?

隐身守侯 提交于 2019-12-30 05:52:47

问题


After switching to Scala 2.10 I get tons of warnings:

reflective access of structural type member method ... should be enabled by making the implicit value language.reflectiveCalls visible

What does it mean?


回答1:


The warning actually tells where to look in the documentation for an explanation:

Test.scala:9: warning: reflective access of structural type member method y should be enabled
by making the implicit value language.reflectiveCalls visible.
This can be achieved by adding the import clause 'import scala.language.reflectiveCalls'
or by setting the compiler option -language:reflectiveCalls.
See the Scala docs for value scala.language.reflectiveCalls for a discussion
why the feature should be explicitly enabled.

Referenced Scaladoc entry (be sure to click the |> arrow to the left to expand the documentation entry).




回答2:


From Scala Docs:

Why control it? Reflection is not available on all platforms. Popular tools such as ProGuard have problems dealing with it. Even where reflection is available, reflective dispatch can lead to surprising performance degradations.

Think about this code that uses an anonymous subclass:

class Student(val id:Int)
val specialStudent = new Student(0) {
   val greeting = "I am a special student with id " + id // Warning: id can be obfuscated
} 

Link to Scala Docs



来源:https://stackoverflow.com/questions/13337976/what-does-reflective-access-of-structural-type-member-method-should-be-enabled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!