Reputation: 1498
I am trying to implement a macro, which would generate new class, extending an existing class, given as an argument:
def impl[T: c.WeakTypeTag](c: Context)(p: c.Expr[T]): c.Expr[() => T] = {
val t = weakTypeOf[T]
q"class Too extends $t {..}; () => new Too()"
}
But trying to run such macro results in :
[error] Main.scala: exception during macro expansion:
[error] scala.ScalaReflectionException: object com.foo.MyClass in compiler mirror not found.
[error] at scala.reflect.internal.Mirrors$RootsBase.staticClass(Mirrors.scala:129)
[error] at scala.reflect.internal.Mirrors$RootsBase.staticClass(Mirrors.scala:29)
[error] at impl(Macro.scala:54)
Upvotes: 2
Views: 89
Reputation: 51648
You seem not to provide enough information to reproduce your error. The following code compiles without errors in 2.13
macros/src/main/scala/Macros.scala
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
object Macros {
def too[T](p: T): () => T = macro impl[T]
def impl[T: c.WeakTypeTag](c: blackbox.Context)(p: c.Expr[T]): c.Expr[() => T] = {
import c.universe._
val t = weakTypeOf[T]
c.Expr[() => T](q"class Too extends $t {}; () => new Too()")
}
}
core/src/main/scala/App.scala
import Macros._
object App {
class MyClass
too(new MyClass) //App$$$Lambda$3/1967205423@77f03bb1
too(new MyClass)() //App$Too$1@77f03bb1
}
//Warning:scalac: {
// class Too extends App.MyClass {
// def <init>() = {
// super.<init>();
// ()
// };
// <empty>
// };
// (() => new Too())
//}
Upvotes: 1