File tree Expand file tree Collapse file tree 2 files changed +9
-8
lines changed
library/src-bootstrapped/scala/quoted Expand file tree Collapse file tree 2 files changed +9
-8
lines changed Original file line number Diff line number Diff line change @@ -23,17 +23,19 @@ abstract class Expr[+T] private[scala] {
23
23
/** Checked cast to a `quoted.Expr[U]` */
24
24
def cast [U ](using tp : scala.quoted.Type [U ])(using qctx : QuoteContext ): scala.quoted.Expr [U ] = asExprOf[U ]
25
25
26
+ /** Checks is the `quoted.Expr[?]` is valid expression of type `X` */
27
+ def isExprOf [X ](using tp : scala.quoted.Type [X ])(using qctx : QuoteContext ): Boolean =
28
+ this .unseal.tpe <:< tp.unseal.tpe
29
+
26
30
/** Convert this to an `quoted.Expr[X]` if this expression is a valid expression of type `X` or throws */
27
31
def asExprOf [X ](using tp : scala.quoted.Type [X ])(using qctx : QuoteContext ): scala.quoted.Expr [X ] = {
28
- val tree = this .unseal
29
- val expectedType = tp.unseal.tpe
30
- if (tree.tpe <:< expectedType)
32
+ if isExprOf[X ] then
31
33
this .asInstanceOf [scala.quoted.Expr [X ]]
32
34
else
33
- throw new scala. tasty.reflect.ExprCastError (
34
- s """ Expr: ${tree .show}
35
- |of type: ${tree .tpe.show}
36
- |did not conform to type: ${expectedType .show}
35
+ throw new tasty.reflect.ExprCastError (
36
+ s """ Expr: ${this .show}
37
+ |of type: ${this .unseal .tpe.show}
38
+ |did not conform to type: ${tp.unseal.tpe .show}
37
39
| """ .stripMargin
38
40
)
39
41
}
Original file line number Diff line number Diff line change 1
1
import scala .quoted ._
2
2
import scala .quoted .staging ._
3
- import scala .quoted .util ._
4
3
import language .experimental .namedTypeArguments
5
4
6
5
/**
You can’t perform that action at this time.
0 commit comments