Can i use scalar from scala script?

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

 ::#! @echo off call scala -classpath *;./libs/* %0 %* goto :eof ::!# //Code relying on pre-compiled code that uses scalap 

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).

+6
source share
1 answer

This is a compiler error:

  • If you run the program inside ide, for example Intellij IDEA, the code runs fine, but no field names were found.
  • If you run it from the command line using scala, you will get the error you specified.

It is not possible for type-safe to ever compile and throw a ClassCastException.

Open the error at https://issues.scala-lang.org/secure/Dashboard.jspa

0
source

Source: https://habr.com/ru/post/914105/


All Articles