Skip to content

Fix Expr.{unlift, unliftOrError} and add missing Unliftables #9513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ abstract class Expr[+T] private[scala] {
def showWith(syntaxHighlight: SyntaxHighlight)(using qctx: QuoteContext): String =
this.unseal.showWith(syntaxHighlight)

/** Return the unlifted value of this expression.
*
* Returns `None` if the expression does not contain a value or contains side effects.
* Otherwise returns the `Some` of the value.
*/
final def unlift[U >: T](using qctx: QuoteContext, unlift: Unliftable[U]): Option[U] =
unlift(this)

/** Return the unlifted value of this expression.
*
* Emits an error and throws if the expression does not contain a value or contains side effects.
* Otherwise returns the value.
*/
final def unliftOrError[U >: T](using qctx: QuoteContext, unlift: Unliftable[U]): U =
unlift(this).getOrElse(report.throwError(s"Expected a known value. \n\nThe value of: $show\ncould not be unlifted using $unlift", this))

/** Pattern matches `this` against `that`. Effectively performing a deep equality check.
* It does the equivalent of
* ```
Expand Down Expand Up @@ -61,6 +45,27 @@ abstract class Expr[+T] private[scala] {

object Expr {

extension [T](expr: Expr[T]):
/** Return the unlifted value of this expression.
*
* Returns `None` if the expression does not contain a value or contains side effects.
* Otherwise returns the `Some` of the value.
*/
def unlift(using qctx: QuoteContext, unlift: Unliftable[T]): Option[T] =
unlift(expr)

/** Return the unlifted value of this expression.
*
* Emits an error and throws if the expression does not contain a value or contains side effects.
* Otherwise returns the value.
*/
def unliftOrError(using qctx: QuoteContext, unlift: Unliftable[T]): T =
def reportError =
val msg = s"Expected a known value. \n\nThe value of: ${expr.show}\ncould not be unlifted using $unlift"
report.throwError(msg, expr)
unlift(expr).getOrElse(reportError)
end extension

/** `e.betaReduce` returns an expression that is functionally equivalent to `e`,
* however if `e` is of the form `((y1, ..., yn) => e2)(x1, ..., xn)`
* then it optimizes this the top most call by returning the result of beta-reducing the application.
Expand Down
Loading