Skip to content

Commit 5132664

Browse files
Merge pull request #8211 from dotty-staging/update-quoted-given-syntax
Update scala.quoted given syntax
2 parents 8725d58 + ceebc33 commit 5132664

File tree

25 files changed

+187
-187
lines changed

25 files changed

+187
-187
lines changed

docs/docs/reference/metaprogramming/erased-terms.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ m.turnedOn.turnedOn // ERROR
3535
// State is must be Off
3636
```
3737

38-
Note that in the code above the actual implicit arguments for `IsOff` are never
38+
Note that in the code above the actual context arguments for `IsOff` are never
3939
used at runtime; they serve only to establish the right constraints at compile
4040
time. As these terms are never used at runtime there is not real need to have
4141
them around, but they still need to be present in some form in the generated
@@ -99,15 +99,15 @@ The following example is an extended implementation of a simple state machine
9999
which can be in a state `On` or `Off`. The machine can change state from `Off`
100100
to `On` with `turnedOn` only if it is currently `Off`, conversely from `On` to
101101
`Off` with `turnedOff` only if it is currently `On`. These last constraint are
102-
captured with the `IsOff[S]` and `IsOn[S]` implicit evidence only exist for
102+
captured with the `IsOff[S]` and `IsOn[S]` given evidence only exist for
103103
`IsOff[Off]` and `IsOn[On]`. For example, not allowing calling `turnedOff` on in
104104
an `Off` state as we would require an evidence `IsOn[Off]` that will not be
105105
found.
106106

107-
As the implicit evidences of `turnedOn` and `turnedOff` are not used in the
107+
As the given evidences of `turnedOn` and `turnedOff` are not used in the
108108
bodies of those functions we can mark them as `erased`. This will remove the
109109
evidence parameters at runtime, but we would still evaluate the `isOn` and
110-
`isOff` implicits that were found as arguments. As `isOn` and `isOff` are not
110+
`isOff` givens that were found as arguments. As `isOn` and `isOff` are not
111111
used except as `erased` arguments, we can mark them as `erased`, hence removing
112112
the evaluation of the `isOn` and `isOff` evidences.
113113

@@ -121,21 +121,21 @@ final class Off extends State
121121
@implicitNotFound("State is must be Off")
122122
class IsOff[S <: State]
123123
object IsOff {
124-
// def isOff will not be called at runtime for turnedOn, the compiler will only require that this evidence exists
125-
implicit def isOff: IsOff[Off] = new IsOff[Off]
124+
// will not be called at runtime for turnedOn, the compiler will only require that this evidence exists
125+
given IsOff[Off] = new IsOff[Off]
126126
}
127127

128128
@implicitNotFound("State is must be On")
129129
class IsOn[S <: State]
130130
object IsOn {
131-
// erased val isOn will not exist at runtime, the compiler will only require that this evidence exists at compile time
132-
erased implicit val isOn: IsOn[On] = new IsOn[On]
131+
// will not exist at runtime, the compiler will only require that this evidence exists at compile time
132+
erased given IsOn[On] = new IsOn[On]
133133
}
134134

135135
class Machine[S <: State] private {
136136
// ev will disappear from both functions
137-
def turnedOn(given erased ev: IsOff[S]): Machine[On] = new Machine[On]
138-
def turnedOff(given erased ev: IsOn[S]): Machine[Off] = new Machine[Off]
137+
def turnedOn(using erased ev: IsOff[S]): Machine[On] = new Machine[On]
138+
def turnedOff(using erased ev: IsOn[S]): Machine[Off] = new Machine[Off]
139139
}
140140

141141
object Machine {

docs/docs/reference/metaprogramming/macros.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ import scala.quoted._
3636
inline def assert(expr: => Boolean): Unit =
3737
${ assertImpl('expr) }
3838

39-
def assertImpl(expr: Expr[Boolean])(given QuoteContext) = '{
39+
def assertImpl(expr: Expr[Boolean])(using QuoteContext) = '{
4040
if (!$expr)
4141
throw new AssertionError(s"failed assertion: ${${ showExpr(expr) }}")
4242
}
4343

44-
def showExpr(expr: Expr[Boolean])(given QuoteContext): Expr[String] =
44+
def showExpr(expr: Expr[Boolean])(using QuoteContext): Expr[String] =
4545
'{ "<some source code>" } // Better implementation later in this document
4646
```
4747

@@ -171,7 +171,7 @@ Indeed, the definition of `reflect` above uses `T` in the next stage, there is a
171171
quote but no splice between the parameter binding of `T` and its
172172
usage. But the code can be rewritten by adding a binding of a `Type[T]` tag:
173173
```scala
174-
def reflect[T, U](f: Expr[T] => Expr[U])(given t: Type[T]): Expr[T => U] =
174+
def reflect[T, U](f: Expr[T] => Expr[U])(using t: Type[T]): Expr[T => U] =
175175
'{ (x: $t) => ${ f('x) } }
176176
```
177177
In this version of `reflect`, the type of `x` is now the result of
@@ -250,7 +250,7 @@ package quoted
250250

251251
object Expr {
252252
...
253-
def apply[T: Liftable](x: T)(given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
253+
def apply[T: Liftable](x: T)(using QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
254254
...
255255
}
256256
```
@@ -304,7 +304,7 @@ analogue of lifting.
304304

305305
Using lifting, we can now give the missing definition of `showExpr` in the introductory example:
306306
```scala
307-
def showExpr[T](expr: Expr[T])(given QuoteContext): Expr[String] = {
307+
def showExpr[T](expr: Expr[T])(using QuoteContext): Expr[String] = {
308308
val code: String = expr.show
309309
Expr(code)
310310
}
@@ -425,12 +425,12 @@ implementation of the `power` function that makes use of a statically known expo
425425
```scala
426426
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
427427

428-
private def powerCode(x: Expr[Double], n: Expr[Int])(given QuoteContext): Expr[Double] =
428+
private def powerCode(x: Expr[Double], n: Expr[Int])(using QuoteContext): Expr[Double] =
429429
n.getValue match
430430
case Some(m) => powerCode(x, m)
431431
case None => '{ Math.pow($x, $y) }
432432

433-
private def powerCode(x: Expr[Double], n: Int)(given QuoteContext): Expr[Double] =
433+
private def powerCode(x: Expr[Double], n: Int)(using QuoteContext): Expr[Double] =
434434
if (n == 0) '{ 1.0 }
435435
else if (n == 1) x
436436
else if (n % 2 == 0) '{ val y = $x * $x; ${ powerCode('y, n / 2) } }
@@ -570,7 +570,7 @@ in a quote context. For this we simply provide `scala.quoted.matching.summonExpr
570570
```scala
571571
inline def setFor[T]: Set[T] = ${ setForExpr[T] }
572572

573-
def setForExpr[T: Type](given QuoteContext): Expr[Set[T]] = {
573+
def setForExpr[T: Type](using QuoteContext): Expr[Set[T]] = {
574574
summonExpr[Ordering[T]] match {
575575
case Some(ord) => '{ new TreeSet[T]()($ord) }
576576
case _ => '{ new HashSet[T] }
@@ -587,7 +587,7 @@ inline method that can calculate either a value of type `Int` or a value of type
587587
```scala
588588
inline def defaultOf(inline str: String) <: Any = ${ defaultOfImpl('str) }
589589

590-
def defaultOfImpl(strExpr: Expr[String])(given QuoteContext): Expr[Any] =
590+
def defaultOfImpl(strExpr: Expr[String])(using QuoteContext): Expr[Any] =
591591
strExpr.value match
592592
case "int" => '{1}
593593
case "string" => '{"a"}
@@ -627,7 +627,7 @@ In `scala.quoted.matching` contains object that can help extract values from `Ex
627627
These could be used in the following way to optimize any call to `sum` that has statically known values.
628628
```scala
629629
inline def sum(args: =>Int*): Int = ${ sumExpr('args) }
630-
private def sumExpr(argsExpr: Expr[Seq[Int]])(given QuoteContext): Expr[Int] = argsExpr.underlyingArgument match {
630+
private def sumExpr(argsExpr: Expr[Seq[Int]])(using QuoteContext): Expr[Int] = argsExpr.underlyingArgument match {
631631
case ConstSeq(args) => // args is of type Seq[Int]
632632
Expr(args.sum) // precompute result of sum
633633
case ExprSeq(argExprs) => // argExprs is of type Seq[Expr[Int]]
@@ -660,7 +660,7 @@ optimize {
660660
```scala
661661
def sum(args: Int*): Int = args.sum
662662
inline def optimize(arg: Int): Int = ${ optimizeExpr('arg) }
663-
private def optimizeExpr(body: Expr[Int])(given QuoteContext): Expr[Int] = body match {
663+
private def optimizeExpr(body: Expr[Int])(using QuoteContext): Expr[Int] = body match {
664664
// Match a call to sum without any arguments
665665
case '{ sum() } => Expr(0)
666666
// Match a call to sum with an argument $n of type Int. n will be the Expr[Int] representing the argument.
@@ -669,7 +669,7 @@ private def optimizeExpr(body: Expr[Int])(given QuoteContext): Expr[Int] = body
669669
case '{ sum(${ExprSeq(args)}: _*) } => sumExpr(args)
670670
case body => body
671671
}
672-
private def sumExpr(args1: Seq[Expr[Int]])(given QuoteContext): Expr[Int] = {
672+
private def sumExpr(args1: Seq[Expr[Int]])(using QuoteContext): Expr[Int] = {
673673
def flatSumArgs(arg: Expr[Int]): Seq[Expr[Int]] = arg match {
674674
case '{ sum(${ExprSeq(subArgs)}: _*) } => subArgs.flatMap(flatSumArgs)
675675
case arg => Seq(arg)
@@ -692,7 +692,7 @@ private def sumExpr(args1: Seq[Expr[Int]])(given QuoteContext): Expr[Int] = {
692692
Sometimes it is necessary to get a more precise type for an expression. This can be achived using the following pattern match.
693693

694694
```scala
695-
def f(exp: Expr[Any])(given QuoteContext) =
695+
def f(exp: Expr[Any])(using QuoteContext) =
696696
expr match
697697
case '{ $x: $t } =>
698698
// If the pattern match succeeds, then there is some type `T` such that
@@ -707,7 +707,7 @@ This might be used to then perform an implicit search as in:
707707
```scala
708708
inline def (sc: StringContext).showMe(args: =>Any*): String = ${ showMeExpr('sc, 'args) }
709709

710-
private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(given qctx: QuoteContext): Expr[String] = {
710+
private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext): Expr[String] = {
711711
argsExpr match {
712712
case ExprSeq(argExprs) =>
713713
val argShowedExprs = argExprs.map {

docs/docs/reference/metaprogramming/staging.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ On the other hand `withQuoteContext` provides a `QuoteContext` without evauating
6666
```scala
6767
package scala.quoted.staging
6868

69-
def run[T](expr:(given QuoteContext) => Expr[T])(given toolbox: Toolbox): T = ...
69+
def run[T](expr: QuoteContext ?=> Expr[T])(using toolbox: Toolbox): T = ...
7070

71-
def withQuoteContext[T](thunk:(given QuoteContext) => T)(given toolbox: Toolbox): T = ...
71+
def withQuoteContext[T](thunk: QuoteContext ?=> T)(using toolbox: Toolbox): T = ...
7272
```
7373

7474
## Create a new Dotty project with staging enabled

docs/docs/reference/metaprogramming/tasty-reflect.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import scala.quoted._
2828

2929
inline def natConst(x: => Int): Int = ${natConstImpl('{x})}
3030

31-
def natConstImpl(x: Expr[Int])(given qctx: QuoteContext): Expr[Int] = {
31+
def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
3232
import qctx.tasty.{_, given}
3333
...
3434
}
@@ -43,7 +43,7 @@ respectively. It will also import all extractors and methods on TASTy Reflect
4343
trees. For example the `Literal(_)` extractor used below.
4444

4545
```scala
46-
def natConstImpl(x: Expr[Int])(given qctx: QuoteContext): Expr[Int] = {
46+
def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
4747
import qctx.tasty.{_, given}
4848
val xTree: Term = x.unseal
4949
xTree match {
@@ -80,7 +80,7 @@ operation expression passed while calling the `macro` below.
8080
```scala
8181
inline def macro(param: => Boolean): Unit = ${ macroImpl('param) }
8282

83-
def macroImpl(param: Expr[Boolean])(given qctx: QuoteContext): Expr[Unit] = {
83+
def macroImpl(param: Expr[Boolean])(using qctx: QuoteContext): Expr[Unit] = {
8484
import qctx.tasty.{_, given}
8585
import util._
8686

docs/docs/reference/other-new-features/tupled-function.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The compiler will synthesize an instance of `TupledFunction[F, G]` if:
2828
* `F` is a function type of arity `N`
2929
* `G` is a function with a single tuple argument of size `N` and it's types are equal to the arguments of `F`
3030
* The return type of `F` is equal to the return type of `G`
31-
* `F` and `G` are the same kind of function (both are `(...) => R` or both are `(given ...) => R`)
31+
* `F` and `G` are the same kind of function (both are `(...) => R` or both are `(...) ?=> R`)
3232
* If only one of `F` or `G` is instantiated the second one is inferred.
3333

3434
Examples
@@ -43,7 +43,7 @@ Examples
4343
* @tparam Args the tuple type with the same types as the function arguments of F
4444
* @tparam R the return type of F
4545
*/
46-
def [F, Args <: Tuple, R](f: F).tupled(given tf: TupledFunction[F, Args => R]): Args => R = tf.tupled(f)
46+
def [F, Args <: Tuple, R](f: F).tupled(using tf: TupledFunction[F, Args => R]): Args => R = tf.tupled(f)
4747
```
4848

4949
`TupledFunction` can be used to generalize the `Function.untupled` methods to functions of any arities ([full example](https://github.com/lampepfl/dotty/blob/master/tests/run/tupled-function-untupled.scala))
@@ -58,7 +58,7 @@ def [F, Args <: Tuple, R](f: F).tupled(given tf: TupledFunction[F, Args => R]):
5858
* @tparam Args the tuple type with the same types as the function arguments of F
5959
* @tparam R the return type of F
6060
*/
61-
def [F, Args <: Tuple, R](f: Args => R).untupled(given tf: TupledFunction[F, Args => R]): F = tf.untupled(f)
61+
def [F, Args <: Tuple, R](f: Args => R).untupled(using tf: TupledFunction[F, Args => R]): F = tf.untupled(f)
6262
```
6363

6464
`TupledFunction` can also be used to generalize the [`Tuple1.compose`](https://github.com/lampepfl/dotty/blob/master/tests/run/tupled-function-compose.scala) and [`Tuple1.andThen`](https://github.com/lampepfl/dotty/blob/master/tests/run/tupled-function-andThen.scala) methods to compose functions of larger arities and with functions that return tuples.
@@ -72,7 +72,7 @@ def [F, Args <: Tuple, R](f: Args => R).untupled(given tf: TupledFunction[F, Arg
7272
* @tparam GArgs the tuple type with the same types as the function arguments of G
7373
* @tparam R the return type of F
7474
*/
75-
def [F, G, FArgs <: Tuple, GArgs <: Tuple, R](f: F).compose(g: G)(given tg: TupledFunction[G, GArgs => FArgs], tf: TupledFunction[F, FArgs => R]): GArgs => R = {
75+
def [F, G, FArgs <: Tuple, GArgs <: Tuple, R](f: F).compose(g: G)(using tg: TupledFunction[G, GArgs => FArgs], tf: TupledFunction[F, FArgs => R]): GArgs => R = {
7676
(x: GArgs) => tf.tupled(f)(tg.tupled(g)(x))
7777
}
7878
```

library/src/scala/internal/quoted/CompileTime.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ object CompileTime {
88

99
/** A term quote is desugared by the compiler into a call to this method */
1010
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.exprQuote`")
11-
def exprQuote[T](x: T): (given QuoteContext) => Expr[T] = ???
11+
def exprQuote[T](x: T): QuoteContext ?=> Expr[T] = ???
1212

1313
/** A term splice is desugared by the compiler into a call to this method */
1414
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.exprSplice`")
15-
def exprSplice[T](x: (given QuoteContext) => Expr[T]): T = ???
15+
def exprSplice[T](x: QuoteContext ?=> Expr[T]): T = ???
1616

1717
/** A type quote is desugared by the compiler into a call to this method */
1818
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.typeQuote`")

library/src/scala/internal/quoted/Expr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object Expr {
3232
* @param qctx the current QuoteContext
3333
* @return None if it did not match, `Some(tup)` if it matched where `tup` contains `Expr[Ti]``
3434
*/
35-
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: scala.quoted.Expr[_])(implicit patternExpr: scala.quoted.Expr[_],
35+
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: scala.quoted.Expr[_])(using patternExpr: scala.quoted.Expr[_],
3636
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
3737
import qctx.tasty.{_, given}
3838
new Matcher.QuoteMatcher[qctx.type].termMatch(scrutineeExpr.unseal, patternExpr.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]

0 commit comments

Comments
 (0)