Created
December 12, 2019 17:02
-
-
Save kmizu/490ee6233662886be6cff037367585a2 to your computer and use it in GitHub Desktop.
Macros
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
import scala.reflect.macros.whitebox.Context | |
import scala.language.experimental.macros | |
object Macros { | |
def reverse(id: Any): Any = macro reverseImpl | |
def reverseImpl(c: Context)(id: c.Expr[Any]) : c.Expr[Any] = { | |
import c.universe._ | |
println(id) | |
println(id.tree) | |
println(id.getClass) | |
println(id.tree.getClass) | |
val Ident(TermName(name)) = id.tree | |
c.Expr[Any](Ident(TermName(name.reverse))) | |
} | |
} |
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
~/work | |
[mizushima]$ scalac Usage.scala | |
Expr[Nothing](foo) | |
foo | |
class scala.reflect.api.Exprs$ExprImpl | |
class scala.reflect.internal.Trees$Ident |
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 Usage { | |
def main(args: Array[String]): Unit = { | |
val foo = 100 | |
val oof = 200 | |
println(Macros.reverse(foo)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment