Scala 2.10 反射:ClassSymbol.isCaseClass 在 Scala 控制台中有效,但在脚本/应用程序中无效 [英] Scala 2.10 reflection: ClassSymbol.isCaseClass works in scala console but not in script/app

查看:50
本文介绍了Scala 2.10 反射:ClassSymbol.isCaseClass 在 Scala 控制台中有效,但在脚本/应用程序中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Scala 2.10.0-M7 中玩反射,偶然发现了 ClassSymbol.isCaseClass 方法,该方法在 Scala 控制台中的行为与预期相同,但在作为 Java 应用程序或作为Scala 脚本.

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.

我像这样定义了 TestScript.scala:

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

我得到这个输出:

isCaseClass: false

如果我改为将代码输入交互式 scala shell 或像这样加载它:

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

如果我编译它并作为标准 Java 应用程序执行它,我再次得到 false 作为 ClassSymbol.isCase 的结果.

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

我错过了什么?scala 控制台环境和 java 运行时环境有什么区别?如何在实际应用中获得正确的结果?

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?

推荐答案

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

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

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

要回答您的最后一个问题,您不会在实际应用程序中使用里程碑.:)

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

更新.自 Scala 2.10.0-RC1 起已修复.

Upd. Fixed since Scala 2.10.0-RC1.

这篇关于Scala 2.10 反射:ClassSymbol.isCaseClass 在 Scala 控制台中有效,但在脚本/应用程序中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆