Created
February 23, 2013 01:53
-
-
Save xeno-by/5017950 to your computer and use it in GitHub Desktop.
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
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