Skip to content

Instantly share code, notes, and snippets.

@rbohrer
Created June 5, 2020 14:41
Show Gist options
  • Save rbohrer/d73630aa8c766ca0d3c585d7640be35f to your computer and use it in GitHub Desktop.
Save rbohrer/d73630aa8c766ca0d3c585d7640be35f to your computer and use it in GitHub Desktop.
Incremental compilation error with macros and inheritance
package example
import macros.Annotate
import InheritExample._
object AnnotateExample extends AnnotateExampleTrait
trait AnnotateExampleTrait {
@Annotate()
val integer: Int = 9
def unit: Unit = println(otherInteger)
}
package example
object InheritExample extends AnnotateExampleTrait {
val otherInteger: Int = 3
}
package example
import org.scalatest.{FlatSpec, Matchers}
class AnnotateTest extends FlatSpec with Matchers {
"Annotation" should "compile and change value" in {
AnnotateExample.integer shouldBe 0
}
}
package macros
import scala.annotation.StaticAnnotation
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
class Annotate() extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro AnnotateImpl.apply
}
class AnnotateImpl(val c: blackbox.Context) {
import c.universe._
def apply(annottees: c.Expr[Any]*): c.Expr[Any] = {
annottees map (_.tree) toList match {
case (vd: ValDef) :: Nil =>
c.Expr[Nothing](q"""val ${vd.name}: ${vd.tpe} = 0""")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment