Skip to content

Instantly share code, notes, and snippets.

@4e6
Created February 4, 2016 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4e6/b4675a93fa41ab9248f6 to your computer and use it in GitHub Desktop.
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.
scala> var c = 0
c: Int = 0
scala> inc(c)
scala> c
res1: Int = 1
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