Reputation: 6693
I was trying to use Toolbox and quasiquote together to do code generation tasks, and faced with StackOverflowError while using AST of object returned from reify(x).tree
, my code is as follows:
abstract class A[T] {def i: T}
class B(val i: Int) extends A[Int]
object A {
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{universe => ru}
import scala.tools.reflect.ToolBox
val javaSeparator = "$"
val curId = new java.util.concurrent.atomic.AtomicInteger()
protected def freshName(prefix: String): TermName = {
newTermName(s"$prefix$javaSeparator${curId.getAndIncrement}")
}
def main(args: Array[String]) {
val b = new B(2)
calculate(b)
}
def calculate(a: A[_]): Unit = {
val toolBox = runtimeMirror(getClass.getClassLoader).mkToolBox()
val i = freshName("i")
val aTree = reify(a).tree
val tree = q"""
val $i = $aTree.i
println($i)
"""
toolBox.eval(tree)
}
}
when I remove type parameter of class A or use def calculate(a: B)
, the toolBox.eval
success and work as expected, i.e. print 2 in console.
I don't quite understand why this happens, can someone explain why class definition with type parameters fails the evaluation?
Upvotes: 1
Views: 537
Reputation: 13048
This is https://issues.scala-lang.org/browse/SI-8833. Until the bug is fixed, you can use a workaround outlined in the comments in JIRA. I've only provided a workaround for 2.11.x, and if you need one for 2.10.x, please leave a comment.
Upvotes: 1