Skip to content

Commit 3248efc

Browse files
committed
Update docs
1 parent 445badd commit 3248efc

File tree

13 files changed

+86
-72
lines changed

13 files changed

+86
-72
lines changed

docs/docs/reference/contextual/by-name-context-parameters.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ trait Codec[T] {
1010
def write(x: T): Unit
1111
}
1212

13-
given intCodec as Codec[Int] = ???
13+
given Codec[Int] as intCodec = ???
1414

15-
given optionCodec[T](using ev: => Codec[T]) as Codec[Option[T]] {
15+
given [T] => (ev: => Codec[T]) => Codec[Option[T]] as optionCodec {
1616
def write(xo: Option[T]) = xo match {
1717
case Some(x) => ev.write(x)
1818
case None =>
@@ -36,7 +36,7 @@ The precise steps for synthesizing an argument for a by-name context parameter o
3636
1. Create a new given of type `T`:
3737

3838
```scala
39-
given lv as T = ???
39+
given T as lv = ???
4040
```
4141
where `lv` is an arbitrary fresh name.
4242

@@ -46,7 +46,7 @@ The precise steps for synthesizing an argument for a by-name context parameter o
4646

4747

4848
```scala
49-
{ given lv as T = E; lv }
49+
{ given T as lv = E; lv }
5050
```
5151

5252
Otherwise, return `E` unchanged.

docs/docs/reference/contextual/context-functions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Context functions are written using `?=>` as the "arrow" sign.
1313
They are applied to synthesized arguments, in
1414
the same way methods with context parameters are applied. For instance:
1515
```scala
16-
given ec as ExecutionContext = ...
16+
given ExecutionContext as ec = ...
1717

1818
def f(x: Int): ExecutionContext ?=> Int = ...
1919

@@ -86,13 +86,13 @@ with context function types as parameters to avoid the plumbing boilerplate
8686
that would otherwise be necessary.
8787
```scala
8888
def table(init: Table ?=> Unit) = {
89-
given t as Table // note the use of a creator application; same as: given t as Table = new Table
89+
given Table as t // note the use of a creator application; same as: given t as Table = new Table
9090
init
9191
t
9292
}
9393

9494
def row(init: Row ?=> Unit)(using t: Table) = {
95-
given r as Row
95+
given Row as r
9696
init
9797
t.add(r)
9898
}

docs/docs/reference/contextual/conversions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ If such an instance `C` is found, the expression `e` is replaced by `C.apply(e)`
3737
primitive number types to subclasses of `java.lang.Number`. For instance, the
3838
conversion from `Int` to `java.lang.Integer` can be defined as follows:
3939
```scala
40-
given int2Integer as Conversion[Int, java.lang.Integer] =
40+
given Conversion[Int, java.lang.Integer] as int2Integer =
4141
java.lang.Integer.valueOf(_)
4242
```
4343

@@ -59,9 +59,9 @@ object Completions {
5959
//
6060
// CompletionArg.fromStatusCode(statusCode)
6161

62-
given fromString as Conversion[String, CompletionArg] = Error(_)
63-
given fromFuture as Conversion[Future[HttpResponse], CompletionArg] = Response(_)
64-
given fromStatusCode as Conversion[Future[StatusCode], CompletionArg] = Status(_)
62+
given Conversion[String, CompletionArg] as fromString = Error(_)
63+
given Conversion[Future[HttpResponse], CompletionArg] as fromFuture = Response(_)
64+
given Conversion[Future[StatusCode], CompletionArg] as fromStatusCode = Status(_)
6565
}
6666
import CompletionArg._
6767

docs/docs/reference/contextual/derivation-macro.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ we need to implement a method `Eq.derived` on the companion object of `Eq` that
2525
produces a quoted instance for `Eq[T]`. Here is a possible signature,
2626

2727
```scala
28-
given derived[T: Type](using Quotes) as Expr[Eq[T]]
28+
given [T: Type] => Quotes => Expr[Eq[T]] as derived
2929
```
3030

3131
and for comparison reasons we give the same signature we had with `inline`:
3232

3333
```scala
34-
inline given derived[T] as (m: Mirror.Of[T]) => Eq[T] = ???
34+
inline given [T] => (m: Mirror.Of[T]) => Eq[T] as derived = ???
3535
```
3636

3737
Note, that since a type is used in a subsequent stage it will need to be lifted
@@ -176,7 +176,7 @@ object Eq {
176176
case '[EmptyTuple] => Nil
177177
}
178178

179-
given derived[T: Type](using q: Quotes) as Expr[Eq[T]] = {
179+
given [T: Type] => (q: Quotes) => Expr[Eq[T]] as derived = {
180180
import quotes.reflect._
181181

182182
val ev: Expr[Mirror.Of[T]] = Expr.summon[Mirror.Of[T]].get

docs/docs/reference/contextual/derivation.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ The `derives` clause generates the following given instances for the `Eq`, `Orde
1919
companion object of `Tree`,
2020

2121
```scala
22-
given [T: Eq] as Eq[Tree[T]] = Eq.derived
23-
given [T: Ordering] as Ordering[Tree] = Ordering.derived
24-
given [T: Show] as Show[Tree] = Show.derived
22+
given [T: Eq] => Eq[Tree[T]] = Eq.derived
23+
given [T: Ordering] => Ordering[Tree] = Ordering.derived
24+
given [T: Show] => Show[Tree] = Show.derived
2525
```
2626

2727
We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Show` instances are _derived instances_.
@@ -179,7 +179,7 @@ we need to implement a method `Eq.derived` on the companion object of `Eq` that
179179
a `Mirror[T]`. Here is a possible implementation,
180180

181181
```scala
182-
inline given derived[T](using m: Mirror.Of[T]) as Eq[T] = {
182+
inline given derived[T] => (m: Mirror.Of[T]) => Eq[T] = {
183183
val elemInstances = summonAll[m.MirroredElemTypes] // (1)
184184
inline m match { // (2)
185185
case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
@@ -278,7 +278,7 @@ object Eq {
278278
}
279279
}
280280

281-
inline given derived[T](using m: Mirror.Of[T]) as Eq[T] = {
281+
inline given [T] => (m: Mirror.Of[T]) => Eq[T] as derived = {
282282
lazy val elemInstances = summonAll[m.MirroredElemTypes]
283283
inline m match {
284284
case s: Mirror.SumOf[T] => eqSum(s, elemInstances)
@@ -309,7 +309,7 @@ In this case the code that is generated by the inline expansion for the derived
309309
following, after a little polishing,
310310

311311
```scala
312-
given derived$Eq[T](using eqT: Eq[T]) as Eq[Opt[T]] =
312+
given [T] => Eq[T] => Eq[Opt[T]] as derived$Eq =
313313
eqSum(summon[Mirror[Opt[T]]],
314314
List(
315315
eqProduct(summon[Mirror[Sm[T]]], List(summon[Eq[T]]))
@@ -326,19 +326,19 @@ As a third example, using a higher level library such as shapeless the type clas
326326
`derived` method as,
327327

328328
```scala
329-
given eqSum[A](using inst: => K0.CoproductInstances[Eq, A]) as Eq[A] {
329+
given [A] => (inst: => K0.CoproductInstances[Eq, A]) => Eq[A] as eqSum {
330330
def eqv(x: A, y: A): Boolean = inst.fold2(x, y)(false)(
331331
[t] => (eqt: Eq[t], t0: t, t1: t) => eqt.eqv(t0, t1)
332332
)
333333
}
334334

335-
given eqProduct[A](using inst: K0.ProductInstances[Eq, A]) as Eq[A] {
335+
given [A] => (inst: K0.ProductInstances[Eq, A]) => Eq[A] as eqProduct {
336336
def eqv(x: A, y: A): Boolean = inst.foldLeft2(x, y)(true: Boolean)(
337337
[t] => (acc: Boolean, eqt: Eq[t], t0: t, t1: t) => Complete(!eqt.eqv(t0, t1))(false)(true)
338338
)
339339
}
340340

341-
inline def derived[A](using gen: K0.Generic[A]) as Eq[A] = gen.derive(eqSum, eqProduct)
341+
inline def derived[A](using gen: K0.Generic[A]): Eq[A] = gen.derive(eqSum, eqProduct)
342342
```
343343

344344
The framework described here enables all three of these approaches without mandating any of them.
@@ -354,7 +354,7 @@ change the code of the ADT itself. To do this, simply define an instance using
354354
as right-hand side. E.g, to implement `Ordering` for `Option` define,
355355

356356
```scala
357-
given [T: Ordering] as Ordering[Option[T]] = Ordering.derived
357+
given [T: Ordering] => Ordering[Option[T]] = Ordering.derived
358358
```
359359

360360
Assuming the `Ordering.derived` method has a context parameter of type `Mirror[T]` it will be satisfied by the

docs/docs/reference/contextual/extension-methods.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ trait SafeDiv:
194194
By the second rule, an extension method can be made available by defining a given instance containing it, like this:
195195

196196
```scala
197-
given ops1 as IntOps // brings safeMod into scope
197+
given IntOps as ops1 // brings safeMod into scope
198198

199199
1.safeMod(2)
200200
```
@@ -209,7 +209,7 @@ object List:
209209
extension [T](xs: List[List[T]])
210210
def flatten: List[T] = xs.foldLeft(Nil: List[T])(_ ++ _)
211211

212-
given [T: Ordering] as Ordering[List[T]]:
212+
given [T: Ordering] => Ordering[List[T]]:
213213
extension (xs: List[T])
214214
def < (ys: List[T]): Boolean = ...
215215
end List

docs/docs/reference/contextual/given-imports.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A special form of import wildcard selector is used to import given instances. Ex
88
```scala
99
object A {
1010
class TC
11-
given tc as TC
11+
given TC as tc
1212
def f(using TC) = ???
1313
}
1414

@@ -60,7 +60,7 @@ For instance, assuming the object
6060

6161
```scala
6262
object Instances {
63-
given intOrd as Ordering[Int]
63+
given Ordering[Int] as intOrd
6464
given listOrd[T: Ordering] as Ordering[List[T]]
6565
given ec as ExecutionContext = ...
6666
given im as Monoid[Int]

docs/docs/reference/contextual/givens.md

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ trait Ord[T] {
1313
extension (x: T) def > (y: T) = compare(x, y) > 0
1414
}
1515

16-
given intOrd as Ord[Int] {
16+
given Ord[Int] {
1717
def compare(x: Int, y: Int) =
1818
if (x < y) -1 else if (x > y) +1 else 0
1919
}
2020

21-
given listOrd[T](using ord: Ord[T]) as Ord[List[T]] {
21+
given [T] => Ord[T] => Ord[List[T]] {
2222

2323
def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match
2424
case (Nil, Nil) => 0
@@ -29,41 +29,42 @@ given listOrd[T](using ord: Ord[T]) as Ord[List[T]] {
2929
if (fst != 0) fst else compare(xs1, ys1)
3030
}
3131
```
32-
This code defines a trait `Ord` with two given instances. `intOrd` defines
33-
a given for the type `Ord[Int]` whereas `listOrd[T]` defines givens
32+
This code defines a trait `Ord` with two given instances. The first instance defines
33+
a given for the type `Ord[Int]`. The second instance defines givens
3434
for `Ord[List[T]]` for all types `T` that come with a given instance for `Ord[T]`
35-
themselves. The `using` clause in `listOrd` defines a condition: There must be a
36-
given of type `Ord[T]` for a given of type `List[Ord[T]]` to exist.
37-
Such conditions are expanded by the compiler to [context
38-
parameters](./using-clauses.html).
35+
themselves. The parts left to the arrows `=>` are type parameters and conditions.
36+
In the example above, there must be a given instance of type `Ord[T]` for a given
37+
instance of type `List[Ord[T]]` to exist. Such conditions are expanded by the compiler
38+
to [context parameters](./using-clauses.html).
3939

40-
## Anonymous Givens
40+
## Named Givens
4141

42-
The name of a given can be left out. So the definitions
43-
of the last section can also be expressed like this:
42+
One can attach a name to a given instance by following its type with an `as`.
43+
For instance, the definitions of the last section can be labelled like this:
4444
```scala
45-
given Ord[Int] { ... }
46-
given [T](using Ord[T]) as Ord[List[T]] { ... }
45+
given Ord[Int] as intOrd { ... }
46+
given [T] => Ord[T] => Ord[List[T]] as listOrd { ... }
4747
```
4848
If the name of a given is missing, the compiler will synthesize a name from
4949
the implemented type(s).
5050

51-
**Note** The name synthesized by the compiler is chosen to be readable and reasonably concise. For instance, the two instances above would get the names:
51+
**Note** The name synthesized by the compiler is chosen to be readable and
52+
reasonably concise. For instance, the two instances in the previous section would get the names:
5253
```scala
5354
given_Ord_Int
5455
given_Ord_List_T
5556
```
56-
The precise rules for synthesizing names are found [here](./relationship-implicits.html#anonymous-given-instances). These rules do not guarantee absence of name conflicts between
57-
given instances of types that are "too similar". To avoid conflicts one can
58-
use named instances.
57+
The precise rules for synthesizing names are found [here](./relationship-implicits.html#anonymous-given-instances).
58+
These rules do not guarantee absence of name conflicts between given instances of types that
59+
are "too similar". To avoid conflicts one needs to use named instances.
5960

6061
**Note** To ensure robust binary compatibility, publicly available libraries should prefer named instances.
6162

6263
## Alias Givens
6364

6465
An alias can be used to define a given instance that is equal to some expression. E.g.:
6566
```scala
66-
given global as ExecutionContext = new ForkJoinPool()
67+
given ExecutionContext as global = new ForkJoinPool()
6768
```
6869
This creates a given `global` of type `ExecutionContext` that resolves to the right
6970
hand side `new ForkJoinPool()`.
@@ -73,7 +74,7 @@ returned for this and all subsequent accesses to `global`. This operation is thr
7374
Alias givens can be anonymous as well, e.g.
7475
```scala
7576
given Position = enclosingTree.position
76-
given (using config: Config) as Factory = MemoizingFactory(config)
77+
given (config: Config) => Factory = MemoizingFactory(config)
7778
```
7879

7980
An alias given can have type parameters and context parameters just like any other given,
@@ -84,11 +85,11 @@ but it can only implement a single type.
8485
Given aliases can have the `inline` and `transparent` modifiers.
8586
Example:
8687
```scala
87-
transparent inline given mkAnnotations[A, T] as Annotations[A, T] = ${
88+
transparent inline given [A, T] => Annotations[A, T] = ${
8889
// code producing a value of a subtype of Annotations
8990
}
9091
```
91-
Since `mkAnnotations` is `transparent`, the type of an application is the type of its right hand side, which can be a proper subtype of the declared result type `Annotations[A, T]`.
92+
Since the given instance is `transparent`, the type of an application is the type of its right hand side, which can be a proper subtype of the declared result type `Annotations[A, T]`.
9293

9394
## Pattern-Bound Given Instances
9495

@@ -98,7 +99,7 @@ Given instances can also appear as pattern bound-variables. Example:
9899
for given Context <- applicationContexts do
99100

100101
pair match
101-
case (ctx as given Context, y) => ...
102+
case (given Context as ctx, y) => ...
102103
```
103104
In the first fragment above, anonymous given instances for class `Context` are established by enumerating over `applicationContexts`. In the second fragment, a given `Context`
104105
instance named `ctx` is established by matching against the first half of the `pair` selector.
@@ -118,7 +119,8 @@ Here is the new syntax for given instances, seen as a delta from the [standard c
118119
```
119120
TmplDef ::= ...
120121
| ‘given’ GivenDef
121-
GivenDef ::= [GivenSig] Type ‘=’ Expr
122-
| [GivenSig] ConstrApps [TemplateBody]
123-
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘as’
122+
GivenDef ::= [GivenParams | `=>`] Type ['as' id] ‘=’ Expr
123+
| [GivenParams] ConstrApps ['as' id] [TemplateBody]
124+
GivenParams ::= [DefTypeParamClause '=>'] {GivenParamClause '=>'}
125+
GivenParamClause ::= `(` DefParams `)` | FunArgTypes
124126
```

docs/docs/reference/contextual/multiversal-equality.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Box[T](x: T) derives CanEqual
9797
By the usual rules of [type class derivation](./derivation.md),
9898
this generates the following `CanEqual` instance in the companion object of `Box`:
9999
```scala
100-
given [T, U](using CanEqual[T, U]) as CanEqual[Box[T], Box[U]] = CanEqual.derived
100+
given [T, U] => CanEqual[T, U] => CanEqual[Box[T], Box[U]] = CanEqual.derived
101101
```
102102
That is, two boxes are comparable with `==` or `!=` if their elements are. Examples:
103103
```scala

0 commit comments

Comments
 (0)