Skip to content

Commit 9479f54

Browse files
committed
Update given syntax
1 parent 669423a commit 9479f54

13 files changed

+132
-132
lines changed

library/src/scala/quoted/Expr.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class Expr[+T] private[scala] {
1616
* Returns `None` if the expression does not contain a value or contains side effects.
1717
* Otherwise returns the `Some` of the value.
1818
*/
19-
final def getValue[U >: T](given qctx: QuoteContext, valueOf: ValueOfExpr[U]): Option[U] = valueOf(this)
19+
final def getValue[U >: T](using qctx: QuoteContext, valueOf: ValueOfExpr[U]): Option[U] = valueOf(this)
2020

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

2929
/** Pattern matches `this` against `that`. Effectively performing a deep equality check.
@@ -34,15 +34,15 @@ class Expr[+T] private[scala] {
3434
* case _ => false
3535
* ```
3636
*/
37-
final def matches(that: Expr[Any])(given qctx: QuoteContext): Boolean =
37+
final def matches(that: Expr[Any])(using qctx: QuoteContext): Boolean =
3838
!scala.internal.quoted.Expr.unapply[Unit, Unit](this)(given that, false, qctx).isEmpty
3939

4040
}
4141

4242
object Expr {
4343

4444
/** Converts a tuple `(T1, ..., Tn)` to `(Expr[T1], ..., Expr[Tn])` */
45-
type TupleOfExpr[Tup <: Tuple] = Tuple.Map[Tup, [X] =>> (given QuoteContext) => Expr[X]]
45+
type TupleOfExpr[Tup <: Tuple] = Tuple.Map[Tup, [X] =>> QuoteContext ?=> Expr[X]]
4646

4747
/** `Expr.betaReduce(f)(x1, ..., xn)` is functionally the same as `'{($f)($x1, ..., $xn)}`, however it optimizes this call
4848
* by returning the result of beta-reducing `f(x1, ..., xn)` if `f` is a known lambda expression.
@@ -52,7 +52,7 @@ object Expr {
5252
* Expr.betaReduce(_): Expr[(T1, ..., Tn) => R] => ((Expr[T1], ..., Expr[Tn]) => Expr[R])
5353
* ```
5454
*/
55-
def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(given tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = {
55+
def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = {
5656
import qctx.tasty.{_, given}
5757
tg.untupled(args => qctx.tasty.internal.betaReduce(f.unseal, args.toArray.toList.map(_.asInstanceOf[QuoteContext => Expr[_]](qctx).unseal)).seal.asInstanceOf[Expr[R]])
5858
}
@@ -62,22 +62,22 @@ object Expr {
6262
*
6363
* `Expr.betaReduceGiven` distributes applications of `Expr` over function arrows
6464
* ```scala
65-
* Expr.betaReduceGiven(_): Expr[(given T1, ..., Tn) => R] => ((Expr[T1], ..., Expr[Tn]) => Expr[R])
65+
* Expr.betaReduceGiven(_): Expr[(T1, ..., Tn) ?=> R] => ((Expr[T1], ..., Expr[Tn]) => Expr[R])
6666
* ```
6767
*/
68-
def betaReduceGiven[F, Args <: Tuple, R, G](f: Expr[F])(given tf: TupledFunction[F, (given Args) => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = {
68+
def betaReduceGiven[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args ?=> R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = {
6969
import qctx.tasty.{_, given}
7070
tg.untupled(args => qctx.tasty.internal.betaReduce(f.unseal, args.toArray.toList.map(_.asInstanceOf[QuoteContext => Expr[_]](qctx).unseal)).seal.asInstanceOf[Expr[R]])
7171
}
7272

7373
/** Returns a null expresssion equivalent to `'{null}` */
74-
def nullExpr: (given QuoteContext) => Expr[Null] = (given qctx) => {
74+
def nullExpr: QuoteContext ?=> Expr[Null] = qctx ?=> {
7575
import qctx.tasty.{_, given}
7676
Literal(Constant(null)).seal.asInstanceOf[Expr[Null]]
7777
}
7878

7979
/** Returns a unit expresssion equivalent to `'{}` or `'{()}` */
80-
def unitExpr: (given QuoteContext) => Expr[Unit] = (given qctx) => {
80+
def unitExpr: QuoteContext ?=> Expr[Unit] = qctx ?=> {
8181
import qctx.tasty.{_, given}
8282
Literal(Constant(())).seal.asInstanceOf[Expr[Unit]]
8383
}
@@ -86,13 +86,13 @@ object Expr {
8686
* Given list of statements `s1 :: s2 :: ... :: Nil` and an expression `e` the resulting expression
8787
* will be equivalent to `'{ $s1; $s2; ...; $e }`.
8888
*/
89-
def block[T](statements: List[Expr[_]], expr: Expr[T])(given qctx: QuoteContext): Expr[T] = {
89+
def block[T](statements: List[Expr[_]], expr: Expr[T])(using qctx: QuoteContext): Expr[T] = {
9090
import qctx.tasty.{_, given}
9191
Block(statements.map(_.unseal), expr.unseal).seal.asInstanceOf[Expr[T]]
9292
}
9393

9494
/** Lift a value into an expression containing the construction of that value */
95-
def apply[T: Liftable](x: T)(given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
95+
def apply[T: Liftable](x: T)(using qctx: QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
9696

9797
/** Lifts this sequence of expressions into an expression of a sequence
9898
*
@@ -106,7 +106,7 @@ object Expr {
106106
* '{ List(${Expr.ofSeq(List(1, 2, 3))}: _*) } // equvalent to '{ List(1, 2, 3) }
107107
* ```
108108
*/
109-
def ofSeq[T](xs: Seq[Expr[T]])(given tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
109+
def ofSeq[T](xs: Seq[Expr[T]])(using tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
110110
import qctx.tasty.{_, given}
111111
Repeated(xs.map(_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
112112
}
@@ -119,7 +119,7 @@ object Expr {
119119
* to an expression equivalent to
120120
* `'{ List($e1, $e2, ...) }` typed as an `Expr[List[T]]`
121121
*/
122-
def ofList[T](xs: Seq[Expr[T]])(given Type[T], QuoteContext): Expr[List[T]] =
122+
def ofList[T](xs: Seq[Expr[T]])(using Type[T], QuoteContext): Expr[List[T]] =
123123
if (xs.isEmpty) '{ Nil } else '{ List(${ofSeq(xs)}: _*) }
124124

125125
/** Lifts this sequence of expressions into an expression of a tuple
@@ -129,7 +129,7 @@ object Expr {
129129
* to an expression equivalent to
130130
* `'{ ($e1, $e2, ...) }` typed as an `Expr[Tuple]`
131131
*/
132-
def ofTuple(seq: Seq[Expr[_]])(given QuoteContext): Expr[Tuple] = {
132+
def ofTuple(seq: Seq[Expr[_]])(using qctx: QuoteContext): Expr[Tuple] = {
133133
seq match {
134134
case Seq() =>
135135
unitExpr
@@ -183,7 +183,7 @@ object Expr {
183183
}
184184

185185
/** Given a tuple of the form `(Expr[A1], ..., Expr[An])`, outputs a tuple `Expr[(A1, ..., An)]`. */
186-
def ofTuple[T <: Tuple: Tuple.IsMappedBy[Expr]: Type](tup: T) (given qctx: QuoteContext): Expr[Tuple.InverseMap[T, Expr]] = {
186+
def ofTuple[T <: Tuple: Tuple.IsMappedBy[Expr]: Type](tup: T)(using qctx: QuoteContext): Expr[Tuple.InverseMap[T, Expr]] = {
187187
import qctx.tasty.{_, given}
188188
val elems: Seq[Expr[_]] = tup.asInstanceOf[Product].productIterator.toSeq.asInstanceOf[Seq[Expr[_]]]
189189
ofTuple(elems).cast[Tuple.InverseMap[T, Expr]]

library/src/scala/quoted/Liftable.scala

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import scala.reflect.ClassTag
88
trait Liftable[T] {
99

1010
/** Lift a value into an expression containing the construction of that value */
11-
def toExpr(x: T): (given QuoteContext) => Expr[T]
11+
def toExpr(x: T): QuoteContext ?=> Expr[T]
1212

1313
}
1414

@@ -31,112 +31,112 @@ object Liftable {
3131

3232
private class PrimitiveLiftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Liftable[T] {
3333
/** Lift a primitive value `n` into `'{ n }` */
34-
def toExpr(x: T) = (given qctx) => {
34+
def toExpr(x: T) = qctx ?=> {
3535
import qctx.tasty.{_, given}
3636
Literal(Constant(x)).seal.asInstanceOf[Expr[T]]
3737
}
3838
}
3939

4040
given ClassIsLiftable[T] : Liftable[Class[T]] = new Liftable[Class[T]] {
4141
/** Lift a `Class[T]` into `'{ classOf[T] }` */
42-
def toExpr(x: Class[T]) = (given qctx) => {
42+
def toExpr(x: Class[T]) = qctx ?=> {
4343
import qctx.tasty.{_, given}
4444
Ref(defn.Predef_classOf).appliedToType(Type(x)).seal.asInstanceOf[Expr[Class[T]]]
4545
}
4646
}
4747

4848
given ClassTagIsLiftable[T: Type] : Liftable[ClassTag[T]] = new Liftable[ClassTag[T]] {
49-
def toExpr(ct: ClassTag[T]): (given QuoteContext) => Expr[ClassTag[T]] =
49+
def toExpr(ct: ClassTag[T]): QuoteContext ?=> Expr[ClassTag[T]] =
5050
'{ ClassTag[T](${Expr(ct.runtimeClass.asInstanceOf[Class[T]])}) }
5151
}
5252

5353
given ArrayIsLiftable[T: Type: Liftable: ClassTag] : Liftable[Array[T]] = new Liftable[Array[T]] {
54-
def toExpr(arr: Array[T]): (given QuoteContext) => Expr[Array[T]] =
54+
def toExpr(arr: Array[T]): QuoteContext ?=> Expr[Array[T]] =
5555
'{ Array[T](${Expr(arr.toSeq)}: _*)(${Expr(summon[ClassTag[T]])}) }
5656
}
5757

5858
given ArrayOfBooleanIsLiftable : Liftable[Array[Boolean]] = new Liftable[Array[Boolean]] {
59-
def toExpr(array: Array[Boolean]): (given QuoteContext) => Expr[Array[Boolean]] =
59+
def toExpr(array: Array[Boolean]): QuoteContext ?=> Expr[Array[Boolean]] =
6060
if (array.length == 0) '{ Array.emptyBooleanArray }
6161
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
6262
}
6363

6464
given ArrayOfByteIsLiftable : Liftable[Array[Byte]] = new Liftable[Array[Byte]] {
65-
def toExpr(array: Array[Byte]): (given QuoteContext) => Expr[Array[Byte]] =
65+
def toExpr(array: Array[Byte]): QuoteContext ?=> Expr[Array[Byte]] =
6666
if (array.length == 0) '{ Array.emptyByteArray }
6767
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
6868
}
6969

7070
given ArrayOfShortIsLiftable : Liftable[Array[Short]] = new Liftable[Array[Short]] {
71-
def toExpr(array: Array[Short]): (given QuoteContext) => Expr[Array[Short]] =
71+
def toExpr(array: Array[Short]): QuoteContext ?=> Expr[Array[Short]] =
7272
if (array.length == 0) '{ Array.emptyShortArray }
7373
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
7474
}
7575

7676
given ArrayOfCharIsLiftable : Liftable[Array[Char]] = new Liftable[Array[Char]] {
77-
def toExpr(array: Array[Char]): (given QuoteContext) => Expr[Array[Char]] =
77+
def toExpr(array: Array[Char]): QuoteContext ?=> Expr[Array[Char]] =
7878
if (array.length == 0) '{ Array.emptyCharArray }
7979
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
8080
}
8181

8282
given ArrayOfIntIsLiftable : Liftable[Array[Int]] = new Liftable[Array[Int]] {
83-
def toExpr(array: Array[Int]): (given QuoteContext) => Expr[Array[Int]] =
83+
def toExpr(array: Array[Int]): QuoteContext ?=> Expr[Array[Int]] =
8484
if (array.length == 0) '{ Array.emptyIntArray }
8585
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
8686
}
8787

8888
given ArrayOfLongIsLiftable : Liftable[Array[Long]] = new Liftable[Array[Long]] {
89-
def toExpr(array: Array[Long]): (given QuoteContext) => Expr[Array[Long]] =
89+
def toExpr(array: Array[Long]): QuoteContext ?=> Expr[Array[Long]] =
9090
if (array.length == 0) '{ Array.emptyLongArray }
9191
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
9292
}
9393

9494
given ArrayOfFloatIsLiftable : Liftable[Array[Float]] = new Liftable[Array[Float]] {
95-
def toExpr(array: Array[Float]): (given QuoteContext) => Expr[Array[Float]] =
95+
def toExpr(array: Array[Float]): QuoteContext ?=> Expr[Array[Float]] =
9696
if (array.length == 0) '{ Array.emptyFloatArray }
9797
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
9898
}
9999

100100
given ArrayOfDoubleIsLiftable : Liftable[Array[Double]] = new Liftable[Array[Double]] {
101-
def toExpr(array: Array[Double]): (given QuoteContext) => Expr[Array[Double]] =
101+
def toExpr(array: Array[Double]): QuoteContext ?=> Expr[Array[Double]] =
102102
if (array.length == 0) '{ Array.emptyDoubleArray }
103103
else '{ Array(${Expr(array(0))}, ${Expr(array.toSeq.tail)}: _*) }
104104
}
105105

106-
given iArrayIsLiftable[T: Type](given ltArray: Liftable[Array[T]]): Liftable[IArray[T]] {
107-
def toExpr(iarray: IArray[T]): (given QuoteContext) => Expr[IArray[T]] =
106+
given iArrayIsLiftable[T: Type](using ltArray: Liftable[Array[T]]): Liftable[IArray[T]] {
107+
def toExpr(iarray: IArray[T]): QuoteContext ?=> Expr[IArray[T]] =
108108
'{ ${ltArray.toExpr(iarray.asInstanceOf[Array[T]])}.asInstanceOf[IArray[T]] }
109109
}
110110

111111
given [T: Type: Liftable] : Liftable[Seq[T]] = new Liftable[Seq[T]] {
112-
def toExpr(xs: Seq[T]): (given QuoteContext) => Expr[Seq[T]] =
112+
def toExpr(xs: Seq[T]): QuoteContext ?=> Expr[Seq[T]] =
113113
Expr.ofSeq(xs.map(summon[Liftable[T]].toExpr))
114114
}
115115

116116
given [T: Type: Liftable] : Liftable[List[T]] = new Liftable[List[T]] {
117-
def toExpr(xs: List[T]): (given QuoteContext) => Expr[List[T]] =
117+
def toExpr(xs: List[T]): QuoteContext ?=> Expr[List[T]] =
118118
Expr.ofList(xs.map(summon[Liftable[T]].toExpr))
119119
}
120120

121121
given [T: Type: Liftable] : Liftable[Set[T]] = new Liftable[Set[T]] {
122-
def toExpr(set: Set[T]): (given QuoteContext) => Expr[Set[T]] =
122+
def toExpr(set: Set[T]): QuoteContext ?=> Expr[Set[T]] =
123123
'{ Set(${Expr(set.toSeq)}: _*) }
124124
}
125125

126126
given [T: Type: Liftable, U: Type: Liftable] : Liftable[Map[T, U]] = new Liftable[Map[T, U]] {
127-
def toExpr(map: Map[T, U]): (given QuoteContext) => Expr[Map[T, U]] =
127+
def toExpr(map: Map[T, U]): QuoteContext ?=> Expr[Map[T, U]] =
128128
'{ Map(${Expr(map.toSeq)}: _*) }
129129
}
130130

131131
given [T: Type: Liftable] : Liftable[Option[T]] = new Liftable[Option[T]] {
132-
def toExpr(x: Option[T]): (given QuoteContext) => Expr[Option[T]] = x match {
132+
def toExpr(x: Option[T]): QuoteContext ?=> Expr[Option[T]] = x match {
133133
case Some(x) => '{ Some[T](${Expr(x)}) }
134134
case None => '{ None: Option[T] }
135135
}
136136
}
137137

138138
given [L: Type: Liftable, R: Type: Liftable] : Liftable[Either[L, R]] = new Liftable[Either[L, R]] {
139-
def toExpr(x: Either[L, R]): (given QuoteContext) => Expr[Either[L, R]] = x match {
139+
def toExpr(x: Either[L, R]): QuoteContext ?=> Expr[Either[L, R]] = x match {
140140
case Left(x) => '{ Left[L, R](${Expr(x)}) }
141141
case Right(x) => '{ Right[L, R](${Expr(x)}) }
142142
}
@@ -289,19 +289,19 @@ object Liftable {
289289
}
290290

291291
given [H: Type: Liftable, T <: Tuple: Type: Liftable] : Liftable[H *: T] = new {
292-
def toExpr(tup: H *: T): (given QuoteContext) => Expr[H *: T] =
292+
def toExpr(tup: H *: T): QuoteContext ?=> Expr[H *: T] =
293293
'{ ${summon[Liftable[H]].toExpr(tup.head)} *: ${summon[Liftable[T]].toExpr(tup.tail)} }
294294
// '{ ${Expr(tup.head)} *: ${Expr(tup.tail)} } // TODO figure out why this fails during CI documentation
295295
}
296296

297297
given Liftable[BigInt] = new Liftable[BigInt] {
298-
def toExpr(x: BigInt): (given QuoteContext) => Expr[BigInt] =
298+
def toExpr(x: BigInt): QuoteContext ?=> Expr[BigInt] =
299299
'{ BigInt(${Expr(x.toByteArray)}) }
300300
}
301301

302302
/** Lift a BigDecimal using the default MathContext */
303303
given Liftable[BigDecimal] = new Liftable[BigDecimal] {
304-
def toExpr(x: BigDecimal): (given QuoteContext) => Expr[BigDecimal] =
304+
def toExpr(x: BigDecimal): QuoteContext ?=> Expr[BigDecimal] =
305305
'{ BigDecimal(${Expr(x.toString)}) }
306306
}
307307

library/src/scala/quoted/Type.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,57 @@ class Type[T <: AnyKind] private[scala] {
77
type `$splice` = T
88

99
/** Show a source code like representation of this type without syntax highlight */
10-
def show(given qctx: QuoteContext): String = qctx.show(this, SyntaxHighlight.plain)
10+
def show(using qctx: QuoteContext): String = qctx.show(this, SyntaxHighlight.plain)
1111

1212
/** Show a source code like representation of this type */
13-
def show(syntaxHighlight: SyntaxHighlight)(given qctx: QuoteContext): String = qctx.show(this, syntaxHighlight)
13+
def show(syntaxHighlight: SyntaxHighlight)(using qctx: QuoteContext): String = qctx.show(this, syntaxHighlight)
1414

1515
}
1616

1717
/** Some basic type tags, currently incomplete */
1818
object Type {
1919

20-
given UnitTag(given qctx: QuoteContext): Type[Unit] = {
20+
given UnitTag(using qctx: QuoteContext): Type[Unit] = {
2121
import qctx.tasty.{_, given}
2222
defn.UnitType.seal.asInstanceOf[quoted.Type[Unit]]
2323
}
2424

25-
given BooleanTag(given qctx: QuoteContext): Type[Boolean] = {
25+
given BooleanTag(using qctx: QuoteContext): Type[Boolean] = {
2626
import qctx.tasty.{_, given}
2727
defn.BooleanType.seal.asInstanceOf[quoted.Type[Boolean]]
2828
}
2929

30-
given ByteTag(given qctx: QuoteContext): Type[Byte] = {
30+
given ByteTag(using qctx: QuoteContext): Type[Byte] = {
3131
import qctx.tasty.{_, given}
3232
defn.ByteType.seal.asInstanceOf[quoted.Type[Byte]]
3333
}
3434

35-
given CharTag(given qctx: QuoteContext): Type[Char] = {
35+
given CharTag(using qctx: QuoteContext): Type[Char] = {
3636
import qctx.tasty.{_, given}
3737
defn.CharType.seal.asInstanceOf[quoted.Type[Char]]
3838
}
3939

40-
given ShortTag(given qctx: QuoteContext): Type[Short] = {
40+
given ShortTag(using qctx: QuoteContext): Type[Short] = {
4141
import qctx.tasty.{_, given}
4242
defn.ShortType.seal.asInstanceOf[quoted.Type[Short]]
4343
}
4444

45-
given IntTag(given qctx: QuoteContext): Type[Int] = {
45+
given IntTag(using qctx: QuoteContext): Type[Int] = {
4646
import qctx.tasty.{_, given}
4747
defn.IntType.seal.asInstanceOf[quoted.Type[Int]]
4848
}
4949

50-
given LongTag(given qctx: QuoteContext): Type[Long] = {
50+
given LongTag(using qctx: QuoteContext): Type[Long] = {
5151
import qctx.tasty.{_, given}
5252
defn.LongType.seal.asInstanceOf[quoted.Type[Long]]
5353
}
5454

55-
given FloatTag(given qctx: QuoteContext): Type[Float] = {
55+
given FloatTag(using qctx: QuoteContext): Type[Float] = {
5656
import qctx.tasty.{_, given}
5757
defn.FloatType.seal.asInstanceOf[quoted.Type[Float]]
5858
}
5959

60-
given DoubleTag(given qctx: QuoteContext): Type[Double] = {
60+
given DoubleTag(using qctx: QuoteContext): Type[Double] = {
6161
import qctx.tasty.{_, given}
6262
defn.DoubleType.seal.asInstanceOf[quoted.Type[Double]]
6363
}

0 commit comments

Comments
 (0)