Scala 2.10 reflection: ClassSymbol.isCaseClass works in scala console but not in script/app

孤者浪人 提交于 2019-12-11 12:47:14

问题


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:


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.



来源:https://stackoverflow.com/questions/12377046/scala-2-10-reflection-classsymbol-iscaseclass-works-in-scala-console-but-not-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!