Created
June 5, 2020 14:41
-
-
Save rbohrer/d73630aa8c766ca0d3c585d7640be35f to your computer and use it in GitHub Desktop.
Incremental compilation error with macros and inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import macros.Annotate | |
import InheritExample._ | |
object AnnotateExample extends AnnotateExampleTrait | |
trait AnnotateExampleTrait { | |
@Annotate() | |
val integer: Int = 9 | |
def unit: Unit = println(otherInteger) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
object InheritExample extends AnnotateExampleTrait { | |
val otherInteger: Int = 3 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import org.scalatest.{FlatSpec, Matchers} | |
class AnnotateTest extends FlatSpec with Matchers { | |
"Annotation" should "compile and change value" in { | |
AnnotateExample.integer shouldBe 0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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