@@ -43,15 +43,19 @@ object FunctionKMacros extends MacroCompat {
43
43
f : c.Expr [(F [α] ⇒ G [α]) forSome { type α }]
44
44
)(
45
45
implicit evF : c.WeakTypeTag [F [_]], evG : c.WeakTypeTag [G [_]]
46
- ): c.Expr [FunctionK [F , G ]] = {
46
+ ): c.Expr [FunctionK [F , G ]] =
47
+ c.Expr [FunctionK [F , G ]](new Lifter [c.type ](c).lift[F , G ](f.tree))
48
+ // ^^note: extra space after c.type to appease scalastyle
49
+
50
+ private [this ] class Lifter [C <: Context ](val c : C ) {
47
51
import c .universe ._
48
52
49
- def unblock (tree : Tree ): Tree = tree match {
53
+ private [ this ] def unblock (tree : Tree ): Tree = tree match {
50
54
case Block (Nil , expr) ⇒ expr
51
55
case _ ⇒ tree
52
56
}
53
57
54
- def punchHole (tpe : Type ): Tree = tpe match {
58
+ private [ this ] def punchHole (tpe : Type ): Tree = tpe match {
55
59
case PolyType (undet :: Nil , underlying : TypeRef ) ⇒
56
60
val α = compatNewTypeName(c, " α" )
57
61
def rebind (typeRef : TypeRef ): Tree =
@@ -71,29 +75,28 @@ object FunctionKMacros extends MacroCompat {
71
75
c.abort(c.enclosingPosition, s " Unexpected type $tpe when lifting to FunctionK " )
72
76
}
73
77
74
- val tree = unblock(f.tree) match {
75
- case q """ ( $param ) => $trans [.. $typeArgs ]( ${ arg : Ident } ) """ if param.name == arg.name ⇒
76
-
77
- typeArgs
78
- .collect { case tt : TypeTree => tt }
79
- .find(_.original != null )
80
- .foreach { param => c.abort(param.pos,
81
- s " type parameter $param must not be supplied when lifting function $trans to FunctionK " )
78
+ def lift [ F [_], G [_]]( tree : Tree )( implicit evF : c. WeakTypeTag [ F [_]], evG : c. WeakTypeTag [ G [_]]) : Tree =
79
+ unblock(tree) match {
80
+ case q """ ( $param ) => $trans [.. $typeArgs ]( ${ arg : Ident } ) """ if param.name == arg.name ⇒
81
+ typeArgs
82
+ .collect { case tt : TypeTree => tt }
83
+ .find(tt => Option (tt.original).isDefined )
84
+ .foreach { param => c.abort(param.pos,
85
+ s " type parameter $param must not be supplied when lifting function $trans to FunctionK " )
82
86
}
83
87
84
- val F = punchHole(evF.tpe)
85
- val G = punchHole(evG.tpe)
88
+ val F = punchHole(evF.tpe)
89
+ val G = punchHole(evG.tpe)
86
90
87
- q """
91
+ q """
88
92
new FunctionK[ $F, $G] {
89
93
def apply[A](fa: $F[A]): $G[A] = $trans(fa)
90
94
}
91
95
"""
92
- case other ⇒
93
- c.abort(other.pos, s " Unexpected tree $other when lifting to FunctionK " )
94
- }
96
+ case other ⇒
97
+ c.abort(other.pos, s " Unexpected tree $other when lifting to FunctionK " )
98
+ }
95
99
96
- c.Expr [FunctionK [F , G ]](tree)
97
100
}
98
101
99
102
}
0 commit comments