Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * implicit conversion ("view"): implicit def f(x: A): B
- * non-view implicit: implicit def f(implicit x: A): B
- *
- * we can summon implicit conversions with `implicitly[A => B]`
- * we can't do that with non-view implicits
- * this macro provides the trait `Conditional[A, B]` that provides precisely that
- */
- package conditional
- import scala.language.experimental.macros
- import scala.reflect.macros.blackbox.Context
- trait Conditional[A, B] {
- def apply(a: A): B
- }
- object Conditional {
- def impl[A : c.WeakTypeTag, B : c.WeakTypeTag](c: Context) = {
- import c.universe._
- val a = weakTypeOf[A]
- val b = weakTypeOf[B]
- val tree = q"""
- new _root_.conditional.Conditional[$a, $b] {
- def apply(a: $a): $b = {
- implicit val _a: $a = a
- _root_.scala.Predef.implicitly[$b]
- }
- }
- """
- c.Expr[Conditional[A, B]](tree)
- }
- implicit def summon[A, B]: Conditional[A, B] = macro impl[A, B]
- }
Add Comment
Please, Sign In to add comment