Created
April 8, 2020 23:35
-
-
Save steinybot/562565bbeb79e30832f5dfe0cd5c5967 to your computer and use it in GitHub Desktop.
Evaluating values in a Scala macro
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.blackbox | |
| object Macro { | |
| def filterRanges(ranges: List[(Char, Char)])(f: Char => Boolean): List[(Char, Char)] = | |
| macro filterRangesImpl | |
| def filterRangesImpl( | |
| c: blackbox.Context | |
| )( | |
| ranges: c.Expr[List[(Char, Char)]] | |
| )( | |
| f: c.Expr[Char => Boolean] | |
| ): c.Expr[List[(Char, Char)]] = { | |
| val r: List[(Char, Char)] = evalUntyped(c)(ranges) | |
| val p: Char => Boolean = evalUntyped(c)(f) | |
| ??? | |
| } | |
| private def evalUntyped[A](c: blackbox.Context)(a: c.Expr[A]): A = | |
| c.eval(cloneUntyped(c)(a)) | |
| @SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements")) | |
| private def cloneUntyped[A](c: blackbox.Context)(a: c.Expr[A]): c.Expr[A] = | |
| c.Expr[A](c.untypecheck(a.tree.duplicate)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment