@@ -13,22 +13,6 @@ abstract class Expr[+T] private[scala] {
13
13
def showWith (syntaxHighlight : SyntaxHighlight )(using qctx : QuoteContext ): String =
14
14
this .unseal.showWith(syntaxHighlight)
15
15
16
- /** Return the unlifted value of this expression.
17
- *
18
- * Returns `None` if the expression does not contain a value or contains side effects.
19
- * Otherwise returns the `Some` of the value.
20
- */
21
- final def unlift [U >: T ](using qctx : QuoteContext , unlift : Unliftable [U ]): Option [U ] =
22
- unlift(this )
23
-
24
- /** Return the unlifted value of this expression.
25
- *
26
- * Emits an error and throws if the expression does not contain a value or contains side effects.
27
- * Otherwise returns the value.
28
- */
29
- final def unliftOrError [U >: T ](using qctx : QuoteContext , unlift : Unliftable [U ]): U =
30
- unlift(this ).getOrElse(report.throwError(s " Expected a known value. \n\n The value of: $show\n could not be unlifted using $unlift" , this ))
31
-
32
16
/** Pattern matches `this` against `that`. Effectively performing a deep equality check.
33
17
* It does the equivalent of
34
18
* ```
@@ -64,6 +48,27 @@ abstract class Expr[+T] private[scala] {
64
48
65
49
object Expr {
66
50
51
+ extension [T ](expr : Expr [T ]):
52
+ /** Return the unlifted value of this expression.
53
+ *
54
+ * Returns `None` if the expression does not contain a value or contains side effects.
55
+ * Otherwise returns the `Some` of the value.
56
+ */
57
+ def unlift (using qctx : QuoteContext , unlift : Unliftable [T ]): Option [T ] =
58
+ unlift(expr)
59
+
60
+ /** Return the unlifted value of this expression.
61
+ *
62
+ * Emits an error and throws if the expression does not contain a value or contains side effects.
63
+ * Otherwise returns the value.
64
+ */
65
+ def unliftOrError (using qctx : QuoteContext , unlift : Unliftable [T ]): T =
66
+ def reportError =
67
+ val msg = s " Expected a known value. \n\n The value of: ${expr.show}\n could not be unlifted using $unlift"
68
+ report.throwError(msg, expr)
69
+ unlift(expr).getOrElse(reportError)
70
+ end extension
71
+
67
72
/** `e.betaReduce` returns an expression that is functionally equivalent to `e`,
68
73
* however if `e` is of the form `((y1, ..., yn) => e2)(x1, ..., xn)`
69
74
* then it optimizes this the top most call by returning the result of beta-reducing the application.
0 commit comments