Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created November 1, 2014 17:47
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 xuwei-k/a7050cefd7a515f4770e to your computer and use it in GitHub Desktop.
Save xuwei-k/a7050cefd7a515f4770e to your computer and use it in GitHub Desktop.
strange behaviors with scala macros
$ scala
Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.reflect.macros.blackbox.Context
import scala.language.experimental.macros
trait T { def t: Int }
abstract class A extends T { override def t: Int = 1}
object Macro { def t(c: Context): c.Expr[Int] = c.universe.reify(2) }
class C extends A { override def t: Int = macro Macro.t }
// Exiting paste mode, now interpreting.
import scala.reflect.macros.blackbox.Context
import scala.language.experimental.macros
defined trait T
defined class A
defined object Macro
defined class C
scala> val c = new C
c: C = C@76c3e77a
scala> c.t
res0: Int = 2
scala> (c: A).t
res1: Int = 1
scala> (c: T).t
res2: Int = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment