17

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?

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
Ivan
  • 63,011
  • 101
  • 250
  • 382
  • 2
    I think it's a result of this SIP https://docs.google.com/document/d/1nlkvpoIRkx7at1qJEZafJwthZ3GeIklTFhqmXMvTX9Q/edit – Noah Nov 12 '12 at 05:31

3 Answers3

17

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).

pme
  • 14,156
  • 3
  • 52
  • 95
Eugene Burmako
  • 13,028
  • 1
  • 46
  • 59
  • 1
    The mystery for me is why does my code need this, it is quite ordinary. – Ivan Nov 12 '12 at 12:00
  • 3
    Would be helpful if you could post some fragments of your code – Eugene Burmako Nov 12 '12 at 15:46
  • It would be too many to post (I shall try to isolate the cause in a piece of model code though). I think the code causing this is a simple "pimp my library" pattern implementation. Just wonder if it could be done without "reflective access"... – Ivan Nov 12 '12 at 17:09
  • 2
    You could share the code with me if it's a github or send an archive to my email (in profile) – Eugene Burmako Nov 12 '12 at 21:30
  • 3
    Dead link now, which is usually why SO encourages providing a summary of the **answer** rather than just linking elsewhere. – Matt Klein Jun 24 '17 at 21:12
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

Gil Zhaiek
  • 21
  • 1
1

I ran into this warning with a function I was using to divide an Option[Double or Long] by 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)
     }

Fixing it simply required adding to the top of the file:

import scala.language.reflectiveCalls
Matt S
  • 11
  • 2