I use scalap
to read the field names of some case classes (as discussed in this question ). Both the case classes and the code that scalap
uses to scalap
them were compiled and placed in a jar file in the classpath.
Now I want to run a script that uses this code, so I followed the instructions and came up with something like
::
which does not work:
java.lang.ClassCastException: scala.None $ cannot be added to scala.Option in scala.tools.nsc.interpreter.ByteCode $ .caseParamNamesForPath (ByteCode. scala: 45) in scala.tools.nsc.interpreter.ProductCompletion.caseNames (ProductCompletion.scala: 22)
However, the code works fine when I compile everything. I played with additional scala
options like -savecompiled
, but that didn't help. Is this a mistake, or can it not work in principle? (If so, can someone explain why not? As I said, the case classes that should be parsed with scalap
are compiled.)
Note. I am using Scala 2.9.1-1.
EDIT
Here is what I'm trying to do (providing an easy way to create multiple instances of the case class):
//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(...)
Note. I switched to Scala 2.9.2, the error remains the same (maybe this is not an error).
source share