Last active
October 13, 2017 11:11
-
-
Save Astrac/204d72d832c93987f4afd8a5da51b23e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
object DeriveFamily { | |
def minimal: Any = macro DeriveFamilyMacros.minimal | |
} | |
object DeriveFamilyMacros { | |
class body(tree: Any) extends StaticAnnotation | |
def bodyImpl(c: Context) = { | |
import c.universe._ | |
val field = c.macroApplication.symbol | |
val bodyAnn = field.annotations.filter(_.tree.tpe <:< typeOf[body]).head | |
bodyAnn.tree.children.tail.head | |
} | |
def minimal(c: Context): c.Tree = { | |
import c.universe._ | |
q"""object Foobar { val _x = "X"; @DeriveFamilyMacros.body(Foobar._x) def x: String = macro DeriveFamilyMacros.bodyImpl }; Foobar""" | |
} | |
} | |
object Working { | |
def x = { | |
val foobar = DeriveFamily.minimal | |
println(foobar.x) | |
} | |
x // Prints "X" | |
} | |
object NotWorking { | |
val foobar = DeriveFamily.minimal | |
println(foobar.x) | |
/* | |
[trace] Stack trace suppressed: run last common/test:compileIncremental for the full output. | |
[error] (common/test:compileIncremental) java.lang.IllegalArgumentException: Could not find proxy for var Foobar$module: runtime.VolatileObjectRef in List(variable Foobar$module, value foobar, Object NotWorking, package <root>) (currentOwner= value <local NotWorking> ) | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment