Skip to content

Commit 7a4ea9f

Browse files
committed
syntax change: delegate for -> given as
1 parent e2f4070 commit 7a4ea9f

12 files changed

+192
-190
lines changed

docs/docs/reference/contextual/conversions.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ layout: doc-page
33
title: "Implicit Conversions"
44
---
55

6-
Implicit conversions are defined by delegates for the `scala.Conversion` class.
6+
Implicit conversions are defined by given instances of the `scala.Conversion` class.
77
This class is defined in package `scala` as follows:
88
```scala
99
abstract class Conversion[-T, +U] extends (T => U)
1010
```
1111
For example, here is an implicit conversion from `String` to `Token`:
1212
```scala
13-
delegate for Conversion[String, Token] {
13+
given as Conversion[String, Token] {
1414
def apply(str: String): Token = new KeyWord(str)
1515
}
1616
```
17-
Using an alias delegate this can be expressed more concisely as:
17+
Using an alias given this can be expressed more concisely as:
1818
```scala
19-
delegate for Conversion[String, Token] = new KeyWord(_)
19+
given as Conversion[String, Token] = new KeyWord(_)
2020
```
2121
An implicit conversion is applied automatically by the compiler in three situations:
2222

@@ -25,19 +25,19 @@ An implicit conversion is applied automatically by the compiler in three situati
2525
3. In an application `e.m(args)` with `e` of type `T`, if `T` does define
2626
some member(s) named `m`, but none of these members can be applied to the arguments `args`.
2727

28-
In the first case, the compiler looks for a delegate for
29-
`scala.Conversion` that maps an argument of type `T` to type `S`. In the second and third
30-
case, it looks for a delegate for `scala.Conversion` that maps an argument of type `T`
28+
In the first case, the compiler looks for a given `scala.Conversion` that maps
29+
an argument of type `T` to type `S`. In the second and third
30+
case, it looks for a given `scala.Conversion` that maps an argument of type `T`
3131
to a type that defines a member `m` which can be applied to `args` if present.
32-
If such a delegate `C` is found, the expression `e` is replaced by `C.apply(e)`.
32+
If such an instance `C` is given, the expression `e` is replaced by `C.apply(e)`.
3333

3434
## Examples
3535

3636
1. The `Predef` package contains "auto-boxing" conversions that map
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-
delegate int2Integer for Conversion[Int, java.lang.Integer] =
40+
given int2Integer as Conversion[Int, java.lang.Integer] =
4141
java.lang.Integer.valueOf(_)
4242
```
4343

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

62-
delegate fromString for Conversion[String, CompletionArg] = Error(_)
63-
delegate fromFuture for Conversion[Future[HttpResponse], CompletionArg] = Response(_)
64-
delegate fromStatusCode for Conversion[Future[StatusCode], CompletionArg] = Status(_)
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(_)
6565
}
6666
import CompletionArg._
6767

docs/docs/reference/contextual/delegates.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
layout: doc-page
3-
title: "Delegates"
3+
title: "Given Instances"
44
---
55

6-
Delegates define "canonical" values of certain types
6+
Given instances (or, simply, "givens") define "canonical" values of certain types
77
that serve for synthesizing arguments to [given clauses](./given-clauses.html). Example:
88

99
```scala
@@ -13,12 +13,14 @@ trait Ord[T] {
1313
def (x: T) > (y: T) = compare(x, y) > 0
1414
}
1515

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

21-
delegate ListOrd[T] for Ord[List[T]] given (ord: Ord[T]) {
21+
given ListOrd[T] as Ord[List[T]] given (ord: Ord[T]) {
22+
where Ord[T], { x <= 0 }
23+
2224
def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match {
2325
case (Nil, Nil) => 0
2426
case (Nil, _) => -1
@@ -29,57 +31,56 @@ delegate ListOrd[T] for Ord[List[T]] given (ord: Ord[T]) {
2931
}
3032
}
3133
```
32-
This code defines a trait `Ord` with two delegate definitions. `IntOrd` defines
33-
a delegate for the type `Ord[Int]` whereas `ListOrd[T]` defines delegates
34-
for `Ord[List[T]]` for all types `T` that come with a delegate for `Ord[T]` themselves.
35-
The `given` clause in `ListOrd` defines an implicit parameter.
34+
This code defines a trait `Ord` with two givens. `IntOrd` defines
35+
a given for the type `Ord[Int]` whereas `ListOrd[T]` defines givens
36+
for `Ord[List[T]]` for all types `T` that come with a given instance for `Ord[T]` themselves.
37+
The `given (ord: Ord[T])` clause in `ListOrd` defines an implicit parameter.
3638
Given clauses are further explained in the [next section](./given-clauses.html).
3739

38-
## Anonymous Delegates
40+
## Anonymous Given Instances
3941

40-
The name of a delegate can be left out. So the delegate definitions
42+
The name of a given instance can be left out. So the given definitions
4143
of the last section can also be expressed like this:
4244
```scala
43-
delegate for Ord[Int] { ... }
44-
delegate [T] for Ord[List[T]] given (ord: Ord[T]) { ... }
45+
given as Ord[Int] { ... }
46+
given [T] as Ord[List[T]] given Ord[T] { ... }
4547
```
46-
If the name of a delegate is missing, the compiler will synthesize a name from
47-
the type(s) in the `for` clause.
48+
If the name of a given is missing, the compiler will synthesize a name from
49+
the type(s) in the `as` clause.
4850

49-
## Alias Delegates
51+
## Alias Givens
5052

51-
An alias can be used to define a delegate that is equal to some expression. E.g.:
53+
An alias can be used to define a given instance that is equal to some expression. E.g.:
5254
```scala
53-
delegate global for ExecutionContext = new ForkJoinPool()
55+
given global as ExecutionContext = new ForkJoinPool()
5456
```
55-
This creates a delegate `global` of type `ExecutionContext` that resolves to the right hand side `new ForkJoinPool()`.
57+
This creates a given `global` of type `ExecutionContext` that resolves to the right
58+
hand side `new ForkJoinPool()`.
5659
The first time `global` is accessed, a new `ForkJoinPool` is created, which is then
5760
returned for this and all subsequent accesses to `global`.
5861

59-
Alias delegates can be anonymous, e.g.
62+
Alias givens can be anonymous, e.g.
6063
```scala
61-
delegate for Position = enclosingTree.position
62-
delegate for Context given (outer: Context) =
63-
outer.withOwner(currentOwner)
64+
given as Position = enclosingTree.position
65+
given as Context given (outer: Context) = outer.withOwner(currentOwner)
6466
```
65-
An alias delegate can have type parameters and given clauses just like any other delegate, but it can only implement a single type.
67+
An alias given can have type parameters and given clauses just like any other given instance, but it can only implement a single type.
6668

67-
## Delegate Instantiation
69+
## Given Instance Initialization
6870

69-
A delegate without type parameters or given clause is instantiated on-demand, the first
71+
A given instance without type parameters or given clause is initialized on-demand, the first
7072
time it is accessed. It is not required to ensure safe publication, which means that
71-
different threads might create different delegates for the same `delegate` clause.
72-
If a `delegate` clause has type parameters or a given clause, a fresh delegate is
73-
created for each reference.
73+
different threads might create different instances for the same `given` definition.
74+
If a `given` definition has type parameters or a given clause, a fresh instance is created for each reference.
7475

7576
## Syntax
7677

77-
Here is the new syntax of delegate clauses, seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html).
78+
Here is the new syntax of given instances, seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html).
7879
```
7980
TmplDef ::= ...
80-
| ‘delegate’ DelegateDef
81-
DelegateDef ::= [id] [DefTypeParamClause] DelegateBody
82-
DelegateBody ::= [‘for’ ConstrApp {‘,’ ConstrApp }] {GivenParamClause} [TemplateBody]
81+
| ‘given’ GivenDef
82+
GivenDef ::= [id] [DefTypeParamClause] GivenBody
83+
GivenBody ::= [‘for’ ConstrApp {‘,’ ConstrApp }] {GivenParamClause} [TemplateBody]
8384
| ‘for’ Type {GivenParamClause} ‘=’ Expr
8485
ConstrApp ::= SimpleConstrApp
8586
| ‘(’ SimpleConstrApp {‘given’ (PrefixExpr | ParArgumentExprs)} ‘)’

docs/docs/reference/contextual/derivation.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ layout: doc-page
33
title: Typeclass Derivation
44
---
55

6-
Typeclass derivation is a way to generate delegates for certain type classes automatically or with minimal code hints. A type class in this sense is any trait or class with a type parameter that describes the type being operated on. Commonly used examples are `Eql`, `Ordering`, `Show`, or `Pickling`. Example:
6+
Typeclass derivation is a way to generate given instances for certain type classes automatically or with minimal code hints. A type class in this sense is any trait or class with a type parameter that describes the type being operated on. Commonly used examples are `Eql`, `Ordering`, `Show`, or `Pickling`. Example:
77
```scala
88
enum Tree[T] derives Eql, Ordering, Pickling {
99
case Branch(left: Tree[T], right: Tree[T])
1010
case Leaf(elem: T)
1111
}
1212
```
13-
The `derives` clause generates delegates for the `Eql`, `Ordering`, and `Pickling` traits in the companion object `Tree`:
13+
The `derives` clause generates given instances for the `Eql`, `Ordering`, and `Pickling` traits in the companion object `Tree`:
1414
```scala
15-
delegate [T: Eql] for Eql[Tree[T]] = Eql.derived
16-
delegate [T: Ordering] for Ordering[Tree[T]] = Ordering.derived
17-
delegate [T: Pickling] for Pickling[Tree[T]] = Pickling.derived
15+
given [T: Eql] as Eql[Tree[T]] = Eql.derived
16+
given [T: Ordering] as Ordering[Tree[T]] = Ordering.derived
17+
given [T: Pickling] as Pickling[Tree[T]] = Pickling.derived
1818
```
1919

2020
### Deriving Types
@@ -40,7 +40,7 @@ The generated typeclass delegates are placed in the companion objects `Labelled`
4040

4141
A trait or class can appear in a `derives` clause if its companion object defines a method named `derived`. The type and implementation of a `derived` method are arbitrary, but typically it has a definition like this:
4242
```scala
43-
def derived[T] given Mirror.Of[T] = ...
43+
def derived[T] where Mirror.Of[T] = ...
4444
```
4545
That is, the `derived` method takes an implicit parameter of (some subtype of) type `Mirror` that defines the shape of the deriving type `T` and it computes the typeclass implementation according
4646
to that shape. A `Mirror` delegate is generated automatically for
@@ -101,10 +101,10 @@ is represented as `T *: Unit` since there is no direct syntax for such tuples: `
101101

102102
### The Generic Typeclass
103103

104-
For every class `C[T_1,...,T_n]` with a `derives` clause, the compiler generates in the companion object of `C` a delegate for `Generic[C[T_1,...,T_n]]` that follows
104+
For every class `C[T_1,...,T_n]` with a `derives` clause, the compiler generates in the companion object of `C` a given instance for `Generic[C[T_1,...,T_n]]` that follows
105105
the outline below:
106106
```scala
107-
delegate [T_1, ..., T_n] for Generic[C[T_1,...,T_n]] {
107+
given [T_1, ..., T_n] as Generic[C[T_1,...,T_n]] {
108108
type Shape = ...
109109
...
110110
}
@@ -122,7 +122,7 @@ would produce:
122122
object Result {
123123
import scala.compiletime.Shape._
124124

125-
delegate [T, E] for Generic[Result[T, E]] {
125+
given [T, E] as Generic[Result[T, E]] {
126126
type Shape = Cases[(
127127
Case[Ok[T], T *: Unit],
128128
Case[Err[E], E *: Unit]
@@ -322,7 +322,7 @@ calling the `error` method defined in `scala.compiletime`.
322322
**Example:** Here is a slightly polished and compacted version of the code that's generated by inline expansion for the derived `Eql` delegate for class `Tree`.
323323

324324
```scala
325-
delegate [T] for Eql[Tree[T]] given (elemEq: Eql[T]) {
325+
given [T] as Eql[Tree[T]] where (elemEq: Eql[T]) {
326326
def eql(x: Tree[T], y: Tree[T]): Boolean = {
327327
val ev = the[Generic[Tree[T]]]
328328
val mx = ev.reflect(x)

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ When is an extension method applicable? There are two possibilities.
3636

3737
- An extension method is applicable if it is visible under a simple name, by being defined
3838
or inherited or imported in a scope enclosing the application.
39-
- An extension method is applicable if it is a member of some delegate that's eligible at the point of the application.
39+
- An extension method is applicable if it is a member of some instance that is given at the point of the application.
4040

4141
As an example, consider an extension method `longestStrings` on `String` defined in a trait `StringSeqOps`.
4242

@@ -48,15 +48,15 @@ trait StringSeqOps {
4848
}
4949
}
5050
```
51-
We can make the extension method available by defining a delegate for `StringSeqOps`, like this:
51+
We can make the extension method available by defining a given of class `StringSeqOps`, like this:
5252
```scala
53-
delegate ops1 for StringSeqOps
53+
given ops1 as StringSeqOps
5454
```
5555
Then
5656
```scala
5757
List("here", "is", "a", "list").longestStrings
5858
```
59-
is legal everywhere `ops1` is available as a delegate. Alternatively, we can define `longestStrings` as a member of a normal object. But then the method has to be brought into scope to be usable as an extension method.
59+
is legal everywhere `ops1` is available as a given instance. Alternatively, we can define `longestStrings` as a member of a normal object. But then the method has to be brought into scope to be usable as an extension method.
6060

6161
```scala
6262
object ops2 extends StringSeqOps
@@ -69,32 +69,32 @@ Assume a selection `e.m[Ts]` where `m` is not a member of `e`, where the type ar
6969
and where `T` is the expected type. The following two rewritings are tried in order:
7070

7171
1. The selection is rewritten to `m[Ts](e)`.
72-
2. If the first rewriting does not typecheck with expected type `T`, and there is a delegate `d`
73-
in either the current scope or in the implicit scope of `T`, and `d` defines an extension
74-
method named `m`, then selection is expanded to `d.m[Ts](e)`.
72+
2. If the first rewriting does not typecheck with expected type `T`, and there is a given instance `i`
73+
in either the current scope or in the implicit scope of `T`, and `i` defines an extension
74+
method named `m`, then selection is expanded to `i.m[Ts](e)`.
7575
This second rewriting is attempted at the time where the compiler also tries an implicit conversion
7676
from `T` to a type containing `m`. If there is more than one way of rewriting, an ambiguity error results.
7777

7878
So `circle.circumference` translates to `CircleOps.circumference(circle)`, provided
79-
`circle` has type `Circle` and `CircleOps` is an eligible delegate (i.e. it is visible at the point of call or it is defined in the companion object of `Circle`).
79+
`circle` has type `Circle` and `CircleOps` is a given instance (i.e. it is visible at the point of call or it is defined in the companion object of `Circle`).
8080

81-
### Delegates for Extension Methods
81+
### Given Instances for Extension Methods
8282

83-
Delegates that define extension methods can also be defined without a `for` clause. E.g.,
83+
Given instances that define extension methods can also be defined without a `for` clause. E.g.,
8484

8585
```scala
86-
delegate StringOps {
86+
given StringOps {
8787
def (xs: Seq[String]) longestStrings: Seq[String] = {
8888
val maxLength = xs.map(_.length).max
8989
xs.filter(_.length == maxLength)
9090
}
9191
}
9292

93-
delegate {
93+
given {
9494
def (xs: List[T]) second[T] = xs.tail.head
9595
}
9696
```
97-
If such delegates are anonymous (as in the second clause), their name is synthesized from the name
97+
If such given instances are anonymous (as in the second clause), their name is synthesized from the name
9898
of the first defined extension method.
9999

100100
### Operators

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ call trees where the same value is passed over and over again in long call chain
99
functions. Given clauses can help here since they enable the compiler to synthesize
1010
repetitive arguments instead of the programmer having to write them explicitly.
1111

12-
For example, given the [delegates](./delegates.md) defined previously,
12+
For example, with the [given instances](./delegates.md) defined previously,
1313
a maximum function that works for any arguments for which an ordering exists can be defined as follows:
1414
```scala
1515
def max[T](x: T, y: T) given (ord: Ord[T]): T =
@@ -73,8 +73,8 @@ def f given (u: Universe) given (x: u.Context) = ...
7373
However, all `given` clauses in a definition must come after any normal parameter clauses.
7474
Multiple given clauses are matched left-to-right in applications. Example:
7575
```scala
76-
delegate global for Universe { type Context = ... }
77-
delegate ctx for global.Context { ... }
76+
given global as Universe { type Context = ... }
77+
given ctx as global.Context { ... }
7878
```
7979
Then the following calls are all valid (and normalize to the last one)
8080
```scala
@@ -84,10 +84,10 @@ f
8484
```
8585
But `f given ctx` would give a type error.
8686

87-
## Summoning Delegates
87+
## Summoning Instances
8888

89-
A method `the` in `Predef` returns the delegate for a given type. For example,
90-
the delegate for `Ord[List[Int]]` is produced by
89+
A method `the` in `Predef` returns the given instance of a specific type. For example,
90+
the given instance for `Ord[List[Int]]` is produced by
9191
```scala
9292
the[Ord[List[Int]]] // reduces to ListOrd given IntOrd
9393
```

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

Lines changed: 8 additions & 8 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-
delegate intCodec for Codec[Int] = ???
13+
given intCodec as Codec[Int] = ???
1414

15-
delegate optionCodec[T] for Codec[Option[T]] given (ev: => Codec[T]) {
15+
given optionCodec[T] as Codec[Option[T]] given (ev: => Codec[T]) {
1616
def write(xo: Option[T]) = xo match {
1717
case Some(x) => ev.write(x)
1818
case None =>
@@ -33,20 +33,20 @@ if this is necessary to prevent an otherwise diverging expansion.
3333

3434
The precise steps for synthesizing an argument for an implicit by-name parameter of type `=> T` are as follows.
3535

36-
1. Create a new delegate for type `T`:
36+
1. Create a new given instance of type `T`:
3737

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

43-
1. This delegate is not immediately available as candidate for argument inference (making it immediately available could result in a loop in the synthesized computation). But it becomes available in all nested contexts that look again for an argument to an implicit by-name parameter.
43+
1. This given instance is not immediately available as candidate for argument inference (making it immediately available could result in a loop in the synthesized computation). But it becomes available in all nested contexts that look again for an argument to an implicit by-name parameter.
4444

45-
1. If this search succeeds with expression `E`, and `E` contains references to the delegate `lv`, replace `E` by
45+
1. If this search succeeds with expression `E`, and `E` contains references to `lv`, replace `E` by
4646

4747

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

5252
Otherwise, return `E` unchanged.
@@ -58,7 +58,7 @@ val s = the[Test.Codec[Option[Int]]](
5858
optionCodec[Int](intCodec))
5959
```
6060

61-
No local delegate was generated because the synthesized argument is not recursive.
61+
No local given instance was generated because the synthesized argument is not recursive.
6262

6363
### Reference
6464

0 commit comments

Comments
 (0)