Skip to content

Commit 1acb7fd

Browse files
committed
added test for case, decribed in #10053
1 parent 043c9b4 commit 1acb7fd

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
partialFunction
2+
partialFunction
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
import scala.quoted._
3+
4+
object X:
5+
6+
def andThen[A,B](a:A)(f: A => B): B =
7+
println("totalFunction")
8+
f(a)
9+
10+
def andThen[A,B](a:A)(f: PartialFunction[A,B]): Option[B] =
11+
println("partialFunction")
12+
f.lift.apply(a)
13+
14+
15+
16+
object Macro {
17+
18+
inline def mThen[A,B](inline x:A=>B):B = ${
19+
mThenImpl[A,B,A=>B,B]('x)
20+
}
21+
22+
inline def mThen[A,B](inline x:PartialFunction[A,B]): Option[B] = ${
23+
mThenImpl[A,B,PartialFunction[A,B],Option[B]]('x)
24+
}
25+
26+
27+
def mThenImpl[A:Type, B:Type, S<:(A=>B) :Type, R:Type](x:Expr[S])(using qctx: QuoteContext):Expr[R]=
28+
import qctx.reflect._
29+
val fun = '{X}.unseal
30+
val wildcard = TypeBounds(quoted.Type[Nothing].unseal.tpe, quoted.Type[Any].unseal.tpe)
31+
val returnType = quoted.Type[(S) => ?].unseal.tpe
32+
val firstPart = Select.overloaded(fun,"andThen",
33+
List(TypeIdent(defn.IntClass).tpe, TypeIdent(defn.IntClass).tpe),
34+
List(Literal(Constant.Int(1))),
35+
quoted.Type[(S) => R].unseal.tpe
36+
)
37+
val r = Apply(firstPart,List(x.unseal))
38+
r.seal.cast[R]
39+
40+
41+
42+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
3+
4+
object Test:
5+
6+
def main(args:Array[String]):Unit =
7+
val x1 = X.andThen(1){case x if (x%2 == 0) => x}
8+
val x2 = Macro.mThen{case x:Int if (x%2 == 0) => x}
9+
10+
11+
12+

0 commit comments

Comments
 (0)