I follow the thread at
practicing compile and execute my customized script. But I notice that I can get the code working in REPL using code similar to that in stackoverflow as below
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
val toolbox = currentMirror.mkToolBox()
import toolbox.u._
val fn = """def foreachline(line: String): String = {println(line); line +"_" }"""
val tree = toolbox.parse("object W { "+fn+"} ")
val symbol = toolbox.define(tree.asInstanceOf[toolbox.u.ImplDef])
val line = "something along the line"; val x = toolbox.eval(q"$symbol.foreachline($line)")
The code above prints original string and return x: Any = something along the line_
without a problem.
Unfortunately, when I try to translate the logic applying to my real program, I find the line below import scala.reflect.runtime.universe.Quasiquote
seems not getting executed even inserting log flag. How to debug this weird situation? (I am sure params passed in containing data not empty seq)
def exec(lines: Seq[String]) {
log.info(s"${lines.length} lines") // here it prints there are e.g. 16340 in seq
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
val toolbox = currentMirror.mkToolBox()
import toolbox.u._
val fnSymbol = toolbox.define(toolbox.parse(s"""
object PerLineScript {
def foreachline(line: String): mypkg.Data = {
import mypkg._
import mypkg.DSL._
val log = org.slf4j.LoggerFactory.getLogger("PerLineScript")
$content
}
}
""").asInstanceOf[toolbox.u.ImplDef])
import scala.reflect.runtime.universe.Quasiquote
// log.info("lines here not printed in log")
val records = lines.map { line => {
log.info("Not seen at all. seemingly not getting executed")
toolbox.eval(q"$fnSymbol.foreachline($line)")
}}.toSeq.asInstanceOf[Seq[mypkg.Data]]
log.info("nothing here shown in log")
}