scalap
を使用して、いくつかのケースクラスのフィールド名を読み込みます(this questionで説明)。ケースクラスとそれらを解析するためにscalap
を使用するコードは、コンパイルされ、クラスパス上のjarファイルに入れられます。スカラスクリプトからスカラップを使用することはできますか?
は、今私はこのコードを使用するスクリプトを実行したいので、私はthe instructionsを踏襲し、
動作しません::#!
@echo off
call scala -classpath *;./libs/* %0 %*
goto :eof
::!#
//Code relying on pre-compiled code that uses scalap
ような何かを思い付いた:
にjava.lang.ClassCastException:スカラーにはキャストできません。scala.tools.nsc.interpreter.ByteCode $ .caseParamNamesForPath(ByteCode。 scala:45)にある の$ 0をキャストすることはできません。 scala.tools.nsc.interpreter.ProductCompletion.caseNames(ProductComple tion.scala:22)
ただし、すべてをコンパイルするとコードはうまく動作します。私はのような追加のscala
オプションで遊んだが、これは役に立たなかった。これはバグですか、これは原理的にはうまくいかないのですか?私が言ったように、scalap
で分析されるケースクラスがコンパイルされます。)
注:私はScala 2.9.1-1を使用します。
ここでEDIT
は、私は基本的に(ケースクラスの複数のインスタンスを作成するための簡単な方法を提供する)やろうとしていますものです:
//This is pre-compiled:
import scala.tools.nsc.interpreter.ProductCompletion
//...
trait MyFactoryTrait[T <: MyFactoryTrait[T] with Product] {
this: T =>
private[this] val copyMethod = this.getClass.getMethods.find(x => x.getName == "copy").get
lazy val productCompletion = new ProductCompletion(this)
/** The names of all specified fields. */
lazy val fieldNames = productCompletion.caseNames //<- provokes the exception (see above)
def createSeq(...):Seq[T] = {
val x = fieldNames map { ... } // <- this method uses the fieldNames value
//[...] invoke copyMethod to create instances
}
// ...
}
//This is pre-compiled too:
case class MyCaseClass(x: Int = 0, y: Int = 0) extends MyFactoryTrait[MyCaseClass]
//This should be interpreted (but crashes):
val seq = MyCaseClass().createSeq(...)
注:私は上へ移動Scala 2.9.2では、エラーは同じままです(おそらくバグではないでしょう)。
もう少しコードを提供してください。 – Edmondo1984
@ Edmondo1984:確かに、上記を参照してください。 –
もっと情報が必要なようです。 createSeqからパラメータを取り出してSeq.empty [T]を返すと、コンパイラのクラッシュを取得できません。 – Edmondo1984