@@ -43,6 +43,42 @@ object QuoteContextImpl {
43
43
44
44
class QuoteContextImpl private (ctx : Context ) extends QuoteContext , QuoteUnpickler , QuoteMatching :
45
45
46
+ extension [T ](self : scala.quoted.Expr [T ]):
47
+ def show : String =
48
+ reflect.TreeMethodsImpl .show(self.asReflectTree)
49
+
50
+ def showAnsiColored : String =
51
+ reflect.TreeMethodsImpl .showAnsiColored(self.asReflectTree)
52
+
53
+ def matches (that : scala.quoted.Expr [Any ]): Boolean =
54
+ treeMatch(self.asReflectTree, that.asReflectTree).nonEmpty
55
+
56
+ def asReflectTree : reflect.Term =
57
+ val expr = self.asInstanceOf [ExprImpl ]
58
+ expr.checkScopeId(QuoteContextImpl .this .hashCode)
59
+ expr.tree
60
+
61
+ end extension
62
+
63
+ extension [X ](self : scala.quoted.Expr [Any ]):
64
+ /** Checks is the `quoted.Expr[?]` is valid expression of type `X` */
65
+ def isExprOf (using scala.quoted.Type [X ]): Boolean =
66
+ reflect.TypeReprMethodsImpl .<:< (self.asReflectTree.tpe)(reflect.TypeRepr .of[X ])
67
+
68
+ /** Convert this to an `quoted.Expr[X]` if this expression is a valid expression of type `X` or throws */
69
+ def asExprOf (using scala.quoted.Type [X ]): scala.quoted.Expr [X ] = {
70
+ if isExprOf[X ] then
71
+ self.asInstanceOf [scala.quoted.Expr [X ]]
72
+ else
73
+ throw Exception (
74
+ s """ Expr cast exception: ${self.show}
75
+ |of type: ${reflect.TypeReprMethodsImpl .show(self.asReflectTree.tpe)}
76
+ |did not conform to type: ${reflect.TypeReprMethodsImpl .show(reflect.TypeRepr .of[X ])}
77
+ | """ .stripMargin
78
+ )
79
+ }
80
+ end extension
81
+
46
82
object reflect extends scala.tasty.Reflection :
47
83
48
84
def rootContext : Context = ctx
@@ -72,15 +108,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
72
108
case _ => false
73
109
def asExpr : scala.quoted.Expr [Any ] =
74
110
if self.isExpr then
75
- new dotty.tools.dotc.quoted. ExprImpl (self, QuoteContextImpl .this .hashCode)
111
+ new ExprImpl (self, QuoteContextImpl .this .hashCode)
76
112
else self match
77
113
case TermTypeTest (self) => throw new Exception (" Expected an expression. This is a partially applied Term. Try eta-expanding the term first." )
78
114
case _ => throw new Exception (" Expected a Term but was: " + self)
79
115
end extension
80
116
81
117
extension [T ](self : Tree )
82
118
def asExprOf (using tp : scala.quoted.Type [T ]): scala.quoted.Expr [T ] =
83
- self.asExpr .asExprOf[T ](using tp )(using QuoteContextImpl . this )
119
+ QuoteContextImpl . this .asExprOf[T ](self.asExpr )(using tp )
84
120
end extension
85
121
86
122
end TreeMethodsImpl
@@ -316,11 +352,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
316
352
object TermMethodsImpl extends TermMethods :
317
353
extension (self : Term ):
318
354
def seal : scala.quoted.Expr [Any ] =
319
- if self.isExpr then new dotty.tools.dotc.quoted. ExprImpl (self, QuoteContextImpl .this .hashCode)
355
+ if self.isExpr then new ExprImpl (self, QuoteContextImpl .this .hashCode)
320
356
else throw new Exception (" Cannot seal a partially applied Term. Try eta-expanding the term first." )
321
357
322
358
def sealOpt : Option [scala.quoted.Expr [Any ]] =
323
- if self.isExpr then Some (new dotty.tools.dotc.quoted. ExprImpl (self, QuoteContextImpl .this .hashCode))
359
+ if self.isExpr then Some (new ExprImpl (self, QuoteContextImpl .this .hashCode))
324
360
else None
325
361
326
362
def tpe : TypeRepr = self.tpe
@@ -1003,7 +1039,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
1003
1039
1004
1040
object TypeTree extends TypeTreeModule :
1005
1041
def of [T <: AnyKind ](using tp : scala.quoted.Type [T ]): TypeTree =
1006
- tp.asInstanceOf [dotty.tools.dotc.quoted. TypeImpl ].typeTree
1042
+ tp.asInstanceOf [TypeImpl ].typeTree
1007
1043
end TypeTree
1008
1044
1009
1045
object TypeTreeMethodsImpl extends TypeTreeMethods :
@@ -1572,7 +1608,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
1572
1608
1573
1609
object TypeRepr extends TypeReprModule :
1574
1610
def of [T <: AnyKind ](using tp : scala.quoted.Type [T ]): TypeRepr =
1575
- tp.asInstanceOf [dotty.tools.dotc.quoted. TypeImpl ].typeTree.tpe
1611
+ tp.asInstanceOf [TypeImpl ].typeTree.tpe
1576
1612
def typeConstructorOf (clazz : Class [? ]): TypeRepr =
1577
1613
if (clazz.isPrimitive)
1578
1614
if (clazz == classOf [Boolean ]) dotc.core.Symbols .defn.BooleanType
@@ -1609,7 +1645,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
1609
1645
def seal : scala.quoted.Type [_] = self.asType
1610
1646
1611
1647
def asType : scala.quoted.Type [? ] =
1612
- new dotty.tools.dotc.quoted. TypeImpl (Inferred (self), QuoteContextImpl .this .hashCode)
1648
+ new TypeImpl (Inferred (self), QuoteContextImpl .this .hashCode)
1613
1649
1614
1650
def =:= (that : TypeRepr ): Boolean = self =:= that
1615
1651
def <:< (that : TypeRepr ): Boolean = self <:< that
@@ -2624,16 +2660,16 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
2624
2660
2625
2661
def unpickleExpr [T ](pickled : String | List [String ], typeHole : (Int , Seq [Any ]) => scala.quoted.Type [? ], termHole : (Int , Seq [Any ], scala.quoted.QuoteContext ) => scala.quoted.Expr [? ]): scala.quoted.Expr [T ] =
2626
2662
val tree = PickledQuotes .unpickleTerm(pickled, typeHole, termHole)(using reflect.rootContext)
2627
- new dotty.tools.dotc.quoted. ExprImpl (tree, hash).asInstanceOf [scala.quoted.Expr [T ]]
2663
+ new ExprImpl (tree, hash).asInstanceOf [scala.quoted.Expr [T ]]
2628
2664
2629
2665
def unpickleType [T <: AnyKind ](pickled : String | List [String ], typeHole : (Int , Seq [Any ]) => scala.quoted.Type [? ], termHole : (Int , Seq [Any ], scala.quoted.QuoteContext ) => scala.quoted.Expr [? ]): scala.quoted.Type [T ] =
2630
2666
val tree = PickledQuotes .unpickleTypeTree(pickled, typeHole, termHole)(using reflect.rootContext)
2631
- new dotty.tools.dotc.quoted. TypeImpl (tree, hash).asInstanceOf [scala.quoted.Type [T ]]
2667
+ new TypeImpl (tree, hash).asInstanceOf [scala.quoted.Type [T ]]
2632
2668
2633
2669
object ExprMatch extends ExprMatchModule :
2634
2670
def unapply [TypeBindings <: Tuple , Tup <: Tuple ](scrutinee : scala.quoted.Expr [Any ])(using pattern : scala.quoted.Expr [Any ]): Option [Tup ] =
2635
- val scrutineeTree = scrutinee.unseal( using QuoteContextImpl .this )
2636
- val patternTree = pattern.unseal( using QuoteContextImpl .this )
2671
+ val scrutineeTree = QuoteContextImpl .this .asReflectTree(scrutinee )
2672
+ val patternTree = QuoteContextImpl .this .asReflectTree(pattern )
2637
2673
treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
2638
2674
end ExprMatch
2639
2675
0 commit comments