Created
July 1, 2012 15:00
-
-
Save retronym/3028685 to your computer and use it in GitHub Desktop.
macro with computed return type
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
scala> def retImpl(c: Context)(b: c.Expr[Boolean]): c.Expr[Any] = { | |
| import c.universe._ | |
| val Literal(Constant(bool: Boolean)) = b.tree | |
| if (bool) c.reify(0) else c.reify(false) | |
| } | |
retImpl: (c: scala.reflect.makro.Context)(b: c.Expr[Boolean])c.Expr[Any] | |
scala> def ret(b: Boolean) = macro retImpl | |
ret: (b: Boolean)Any | |
scala> ret(true) | |
res3: Int = 0 | |
scala> ret(false) | |
res4: Boolean = false | |
scala> ret(false) | true | |
res5: Boolean = true | |
scala> ret(false) * 1 | |
<console>:16: error: value * is not a member of Boolean | |
ret(false) * 1 | |
^ | |
scala> ret(true) * 1 | |
res7: Int = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment