Skip to content

Instantly share code, notes, and snippets.

@xeno-by
Created February 23, 2013 01:53
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 xeno-by/5017950 to your computer and use it in GitHub Desktop.
Save xeno-by/5017950 to your computer and use it in GitHub Desktop.
import scala.reflect.macros.Context
object Macros {
implicit class NumericExtensions[T : Numeric](self: T) {
def squared: T = macro squaredImpl[T]
}
def squaredImpl[T : c.WeakTypeTag](c: Context): c.Expr[T] = {
import c.universe._
val Apply(_, List(selfTree)) = c.prefix.tree
val self = c.Expr[T](selfTree)
val ev = c.Expr[Numeric[T]](c.inferImplicitValue(weakTypeTag[Numeric[T]].tpe))
reify {
implicit val n = ev.splice
self.splice * self.splice
}
}
}
02:52 ~/Projects/Kepler_snippet01/sandbox (topic/snippet01)$ scalac Test.scala
Test.scala:15: error: value * is not a member of type parameter T
self.splice * self.splice
^
one error found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment