Skip to content

Instantly share code, notes, and snippets.

@retronym
Created July 7, 2015 08:46
Show Gist options
  • Save retronym/8501b1b3b113f4386cda to your computer and use it in GitHub Desktop.
Save retronym/8501b1b3b113f4386cda to your computer and use it in GitHub Desktop.
% tail -n100 sandbox/{macro,client}.scala && scalac sandbox/macro.scala && scalac sandbox/client.scala
==> sandbox/macro.scala <==
class TC[T]
case class C(nodes: List[C])
object Macro {
import language.experimental.macros
import reflect.macros.whitebox.Context
def impl[T <: C: c.WeakTypeTag](c: Context): c.Expr[TC[T]] = {
import c.universe._
def proposal = q"""{
implicit def m: TC[C] = ???
implicitly[TC[List[C]]]
???
}"""
println("typechecked: " + c.typeCheck(proposal, silent = true, withMacrosDisabled = true))
c.Expr[TC[T]](proposal)
}
implicit def m[T <: C]: TC[T] = macro impl[T]
implicit def listTC[T: TC]: TC[List[T]] = new TC[List[T]]
}
==> sandbox/client.scala <==
class Client {
import Macro._
implicitly[TC[List[C]]]
}
warning: there was one deprecation warning; re-run with -deprecation for details
one warning found
typechecked: <empty>
typechecked: {
implicit def m: TC[C] = scala.this.Predef.???;
scala.this.Predef.implicitly[TC[List[C]]](Macro.listTC[C](m));
scala.this.Predef.???
}
%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment