related?
Created
November 1, 2014 17:47
-
-
Save xuwei-k/a7050cefd7a515f4770e to your computer and use it in GitHub Desktop.
strange behaviors with scala macros
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 | |
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