Created
February 4, 2016 19:54
-
-
Save 4e6/b4675a93fa41ab9248f6 to your computer and use it in GitHub Desktop.
Variable `counter` passed to a macros behaves as if it was passed by-reference.
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
scala> var c = 0 | |
c: Int = 0 | |
scala> inc(c) | |
scala> c | |
res1: Int = 1 |
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
def inc(counter: Int): Unit = macro incImpl | |
def incImpl(c: Context)(counter: c.Expr[Int]): c.Expr[Unit] = { | |
import c.universe._ | |
c.Expr[Unit](q"$counter += 1") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment