File tree Expand file tree Collapse file tree 1 file changed +16
-7
lines changed
library/src-bootstrapped/scala/quoted Expand file tree Collapse file tree 1 file changed +16
-7
lines changed Original file line number Diff line number Diff line change @@ -25,17 +25,26 @@ abstract class Expr[+T] private[scala] {
25
25
/** Checked cast to a `quoted.Expr[U]` */
26
26
def cast [U ](using tp : scala.quoted.Type [U ])(using qctx : QuoteContext ): scala.quoted.Expr [U ] = asExprOf[U ]
27
27
28
+ /** Checks is the `quoted.Expr[?]` is valid expression of type `X` */
29
+ def isExprOf [X ](using tp : scala.quoted.Type [X ])(using qctx : QuoteContext ): Boolean =
30
+ this .unseal.tpe <:< tp.unseal.tpe
31
+
32
+ /** Convert this to an `Some[quoted.Expr[X]]` if this expression is a valid expression of type `X`.
33
+ * Otherwise returns None.
34
+ */
35
+ def toExprOf [X ](using tp : scala.quoted.Type [X ])(using qctx : QuoteContext ): Option [scala.quoted.Expr [X ]] =
36
+ if isExprOf[X ] then Some (this .asInstanceOf [scala.quoted.Expr [X ]])
37
+ else None
38
+
28
39
/** Convert this to an `quoted.Expr[X]` if this expression is a valid expression of type `X` or throws */
29
40
def asExprOf [X ](using tp : scala.quoted.Type [X ])(using qctx : QuoteContext ): scala.quoted.Expr [X ] = {
30
- val tree = this .unseal
31
- val expectedType = tp.unseal.tpe
32
- if (tree.tpe <:< expectedType)
41
+ if isExprOf[X ] then
33
42
this .asInstanceOf [scala.quoted.Expr [X ]]
34
43
else
35
- throw new scala. tasty.reflect.ExprCastError (
36
- s """ Expr: ${tree .show}
37
- |of type: ${tree .tpe.show}
38
- |did not conform to type: ${expectedType .show}
44
+ throw new tasty.reflect.ExprCastError (
45
+ s """ Expr: ${this .show}
46
+ |of type: ${this .unseal .tpe.show}
47
+ |did not conform to type: ${tp.unseal.tpe .show}
39
48
| """ .stripMargin
40
49
)
41
50
}
You can’t perform that action at this time.
0 commit comments