切换到Scala 2.10后,我收到了大量的警告:
结构类型成员方法的反射访问...应该通过使隐式值language.reflectiveCalls可见来启用
这是什么意思?
答案 0 :(得分:16)
警告实际上告诉我们在文档中查找解释的位置:
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(请务必点击左侧的|>箭头展开文档条目。)
答案 1 :(得分:1)
我用一个函数将 Option[Double or Long]
除以 100 时遇到了这个警告:
def safeDivideBy100[T <: AnyVal { def toDouble: Double }](number: Option[T]): Option[Double] =
number match {
case None => None
case Some(x) => Some(x.toDouble / 100)
}
修复它只需添加到文件的顶部:
import scala.language.reflectiveCalls
答案 2 :(得分:0)
来自Scala文档:
为什么要控制它?所有平台都没有反射功能。像ProGuard这样的流行工具在处理它时遇到了问题。即使反射可用,反射调度也会导致令人惊讶的性能下降。
考虑一下使用匿名子类的代码:
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
}