Scala 2.10 reflection: ClassSymbol.isCaseClass wor

2019-08-02 13:26发布

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?

1条回答
三岁会撩人
2楼-- · 2019-08-02 14:14

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.

查看更多
登录 后发表回答