繁体   English   中英

“应该启用结构类型成员方法的反射访问......”警告在Scala中是什么意思?

[英]What does "reflective access of structural type member method should be enabled..." warning mean in Scala?

切换到 Scala 2.10 后,我收到了大量警告:

结构类型成员方法的反射访问...应该通过使隐式值 language.reflectiveCalls 可见来启用

这是什么意思?

警告实际上告诉在文档中查找解释的位置:

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.

引用的 Scaladoc 条目(请务必单击左侧的 |> 箭头以展开文档条目)。

来自 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
} 

链接到 Scala 文档

我用我用来将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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM