1

I am playing around with reflection in Scala 2.10.0-M7 and stumbled upon the ClassSymbol.isCaseClass method which behaves like expected in the scala console but not when executed as a java application or as a scala script.

I've defined TestScript.scala like this:

import reflect.runtime.currentMirror

case class TestCase(foo: String)

object Test {
  def main(args: Array[String]) {
    val classSymbol = currentMirror.reflect(new TestCase("foo")).symbol
    val isCaseClass = classSymbol.isCaseClass
    println(s"isCaseClass: $isCaseClass")
  }
}

Test.main(Array())

If I execute it on the command line calling

$ scala TestScript.scala

I get this output:

isCaseClass: false

If I instead input the code into the interactive scala shell or load it like this:

scala> :load TestScript.scala

I get the following correct output:

Loading TestScript.scala...
import reflect.runtime.currentMirror
defined class TestCase
defined module Test
isCaseClass: true

If I compile it and execute it as a standard Java app I get false as result for ClassSymbol.isCase again.

What am I missing? What are the differences between the scala console environment and the java runtime environment? How can I get the correct result in a real application?

sven
  • 4,161
  • 32
  • 33

1 Answers1

2

https://issues.scala-lang.org/browse/SI-6277

val classSymbol = cm.reflect(new TestCase("foo")).symbol

{ classSymbol.typeSignature }
val isCaseClass = classSymbol.isCaseClass
println(s"isCaseClass: $isCaseClass")

Edit: to answer your last question, you wouldn't be using a milestone in a real application. :)

Upd. Fixed since Scala 2.10.0-RC1.

Eugene Burmako
  • 13,028
  • 1
  • 46
  • 59
som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • It works if executed as a java application, but surprisingly not if executed as a scala script. But as the script was only for trying around this is no blocker for me. Regarding your edit: of course I don't use a milestone for a productive app ;-) With "real" I only meant a compiled artifact (e.g. jar) running on the JVM. – sven Sep 12 '12 at 07:16