Skip to content

Commit a02adf4

Browse files
committed
Update docs
1 parent 091b8f3 commit a02adf4

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ object IsOn {
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
```

0 commit comments

Comments
 (0)