This script uses reflection to find the type signature of a constructor. It contains the same code inside an object and at the top level:
// Scala 2.11.1
case class Dirigible(cubicFeet: Int)
object Object {
val u = scala.reflect.runtime.universe
val ctor = u.weakTypeOf[Dirigible].decl(u.termNames.CONSTRUCTOR).typeSignature
def run() {
println(ctor)
}
}
Object.run()
val u = scala.reflect.runtime.universe
val ctor = u.weakTypeOf[Dirigible].decl(u.termNames.CONSTRUCTOR).typeSignature
println(ctor)
Here is the output:
(cubicFeet: Int)$anon.this.Dirigible
<notype>
Why does the top-level code fail while the code inside Object
works?
The same failure occurs if I put the println
inside a top-level def
and call it from the top level.
The top-level code works if I run the file in the REPL via scala -i refltest.scala
. That's to be expected, since the REPL puts everything into an object. What I don't understand is, why does code's being inside an object affect the results of reflection?