Skip to content

Rename ValueOfExpr to Unliftable #8503

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
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
6 changes: 3 additions & 3 deletions library/src/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Expr[+T] private[scala] {
* Returns `None` if the expression does not contain a value or contains side effects.
* Otherwise returns the `Some` of the value.
*/
final def getValue[U >: T](using qctx: QuoteContext, valueOf: ValueOfExpr[U]): Option[U] = valueOf(this)
final def getValue[U >: T](using qctx: QuoteContext, unlift: Unliftable[U]): Option[U] = unlift(this)

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

/** Pattern matches `this` against `that`. Effectively performing a deep equality check.
* It does the equivalent of
Expand Down

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions library/src/scala/quoted/Value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object Value {
* }
* ```
*/
def unapply[T](expr: Expr[T])(using valueOf: ValueOfExpr[T], qxtc: QuoteContext): Option[T] =
valueOf(expr)
def unapply[T](expr: Expr[T])(using unlift: Unliftable[T], qxtc: QuoteContext): Option[T] =
unlift(expr)

}
2 changes: 1 addition & 1 deletion library/src/scala/quoted/Values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Values {
* }
* ```
*/
def unapply[T](exprs: Seq[Expr[T]])(using valueOf: ValueOfExpr[T], qctx: QuoteContext): Option[Seq[T]] =
def unapply[T](exprs: Seq[Expr[T]])(using unlift: Unliftable[T], qctx: QuoteContext): Option[Seq[T]] =
exprs.foldRight(Option(List.empty[T])) { (elem, acc) =>
(elem, acc) match {
case (Value(value), Some(lst)) => Some(value :: lst)
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/matching/ValueSeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object ValueSeq {
* ```
*/
@deprecated("use scala.quoted.Varargs(scala.quoted.Value(_)) instead", "0.23.0")
def unapply[T](expr: Expr[Seq[T]])(using valueOf: ValueOfExpr[T], qctx: QuoteContext): Option[Seq[T]] =
def unapply[T](expr: Expr[Seq[T]])(using unlift: Unliftable[T], qctx: QuoteContext): Option[Seq[T]] =
import scala.quoted.Const
expr match
case Varargs(Values(elems)) => Some(elems)
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/quoted/matching/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package object matching {
@deprecated("use scala.quoted.Value instead", "0.23.0")
val Value: quoted.Value.type = quoted.Value

@deprecated("use scala.quoted.ValueOfExpr instead", "0.23.0")
val ValueOfExpr: quoted.ValueOfExpr.type = quoted.ValueOfExpr
@deprecated("use scala.quoted.Value instead", "0.23.0")
val ValueOfExpr: quoted.Value.type = quoted.Value

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object E {

def impl[T: Type](x: Expr[E[T]]) (using QuoteContext): Expr[T] = x.value.lift

implicit def ev1[T: Type]: ValueOfExpr[E[T]] = new ValueOfExpr {
implicit def ev1[T: Type]: Unliftable[E[T]] = new Unliftable {
def apply(x: Expr[E[T]]) (using QuoteContext): Option[E[T]] = x match {
case '{ I(${Const(n)}) } => Some(I(n).asInstanceOf[E[T]])
case '{ Plus[T](${Value(x)}, ${Value(y)})(using $op) } if op.matches('{Plus2.IPlus}) => Some(Plus(x, y)(using Plus2.IPlus.asInstanceOf[Plus2[T]]).asInstanceOf[E[T]])
Expand All @@ -18,7 +18,7 @@ object E {
}

object Value {
def unapply[T, U >: T](expr: Expr[T])(using ValueOfExpr[U], QuoteContext): Option[U] = expr.getValue
def unapply[T, U >: T](expr: Expr[T])(using Unliftable[U], QuoteContext): Option[U] = expr.getValue
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/i7964/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import scala.quoted._

enum Num { // TODO derive a quoted.ValueOfExpr
enum Num { // TODO derive a quoted.Unliftable
case One
case Two
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object E {
def impl[T: Type](expr: Expr[E[T]]) (using QuoteContext): Expr[T] =
expr.value.lift

implicit def ev1[T: Type]: ValueOfExpr[E[T]] = new ValueOfExpr { // TODO use type class derivation
implicit def ev1[T: Type]: Unliftable[E[T]] = new Unliftable { // TODO use type class derivation
def apply(x: Expr[E[T]]) (using QuoteContext): Option[E[T]] = (x match {
case '{ I(${Const(n)}) } => Some(I(n))
case '{ D(${Const(n)}) } => Some(D(n))
Expand All @@ -22,7 +22,7 @@ object E {
}

object Value {
def unapply[T, U >: T](expr: Expr[T])(using ValueOfExpr[U], QuoteContext): Option[U] = expr.getValue
def unapply[T, U >: T](expr: Expr[T])(using Unliftable[U], QuoteContext): Option[U] = expr.getValue
}

}
Expand Down
8 changes: 4 additions & 4 deletions tests/run-staging/quote-valueof-list.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Test {

def main(args: Array[String]): Unit = withQuoteContext {

implicit def ValueOfExprInt: ValueOfExpr[Int] = new {
implicit def UnliftableInt: Unliftable[Int] = new {
def apply(n: Expr[Int])(using QuoteContext): Option[Int] = n match {
case '{ 0 } => Some(0)
case '{ 1 } => Some(1)
Expand All @@ -16,15 +16,15 @@ object Test {
}
}

implicit def ValueOfExprBoolean: ValueOfExpr[Boolean] = new ValueOfExpr[Boolean] {
implicit def UnliftableBoolean: Unliftable[Boolean] = new Unliftable[Boolean] {
def apply(b: Expr[Boolean])(using QuoteContext): Option[Boolean] = b match {
case '{ true } => Some(true)
case '{ false } => Some(false)
case _ => None
}
}

implicit def ValueOfExprList[T: ValueOfExpr: Type]: ValueOfExpr[List[T]] = new {
implicit def UnliftableList[T: Unliftable: Type]: Unliftable[List[T]] = new {
def apply(xs: Expr[List[T]])(using QuoteContext): Option[List[T]] = (xs: Expr[Any]) match {
case '{ ($xs1: List[T]).::($x) } =>
for { head <- x.getValue; tail <- xs1.getValue }
Expand All @@ -34,7 +34,7 @@ object Test {
}
}

implicit def ValueOfExprOption[T: ValueOfExpr: Type]: ValueOfExpr[Option[T]] = new {
implicit def UnliftableOption[T: Unliftable: Type]: Unliftable[Option[T]] = new {
def apply(expr: Expr[Option[T]])(using QuoteContext): Option[Option[T]] = expr match {
case '{ Some[T]($x) } => for (v <- x.getValue) yield Some(v)
case '{ None } => Some(None)
Expand Down