Skip to content

Instantly share code, notes, and snippets.

@patrick-premont
Last active June 29, 2017 17:22
Show Gist options
  • Save patrick-premont/01c5847958e1776d7748bb9570ee376e to your computer and use it in GitHub Desktop.
Save patrick-premont/01c5847958e1776d7748bb9570ee376e to your computer and use it in GitHub Desktop.
# Illustrates the compile-time dispatch that has to occur with macro overrides.
# You can install Ammonite-REPL (http://www.lihaoyi.com/Ammonite/#Ammonite-REPL), or use scala instead of amm
$ amm
Loading...
Welcome to the Ammonite Repl 0.9.3
(Scala 2.12.2 Java 1.8.0_121)
@ import scala.reflect.macros.blackbox
import scala.reflect.macros.blackbox
@ import scala.language.experimental.macros
import scala.language.experimental.macros
@ def fooA(c: blackbox.Context) = {
import c.universe._
q""" "A" """
}
defined function fooA
@ def fooB(c: blackbox.Context) = {
import c.universe._
q""" "B" """
}
defined function fooB
@ class A {
def foo: String = macro fooA
}
defined class A
@ class B extends A {
override def foo: String = macro fooB
}
defined class B
@ (new A).foo
res6: String = "A"
@ (new B).foo
res7: String = "B"
@ ((new B): A).foo
res8: String = "A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment