You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Change contextual docs to reflect three changes to `given` syntax:
- put `given` inside the parentheses of parameters and arguments, as was suggested by @smarter.
- change given import syntax to make `given` an import selector
- change `the` to `theGiven`.
All of these should discussed in separate issues.
A bound like `: Ord` on a type parameter `T` of a method or class indicates an implicit parameter `given Ord[T]`. The implicit parameter(s) generated from context bounds come last in the definition of the containing method or class. E.g.,
12
+
A bound like `: Ord` on a type parameter `T` of a method or class indicates an implicit parameter `(given Ord[T])`. The implicit parameter(s) generated from context bounds come last in the definition of the containing method or class. E.g.,
13
13
```scala
14
-
deff[T:C1:C2, U:C3](x: T)given(y: U, z: V):R
14
+
deff[T:C1:C2, U:C3](x: T)(giveny:U, z: V):R
15
15
```
16
16
would expand to
17
17
```scala
18
-
deff[T, U](x: T)given(y: U, z: V)givenC1[T], C2[T], C3[U]:R
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/given-clauses.md
+31-28Lines changed: 31 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,15 +12,15 @@ repetitive arguments instead of the programmer having to write them explicitly.
12
12
For example, with the [given instances](./delegates.md) defined previously,
13
13
a maximum function that works for any arguments for which an ordering exists can be defined as follows:
14
14
```scala
15
-
defmax[T](x: T, y: T)given(ord: Ord[T]):T=
15
+
defmax[T](x: T, y: T)(givenord:Ord[T]):T=
16
16
if (ord.compare(x, y) <0) y else x
17
17
```
18
18
Here, `ord` is an _implicit parameter_ introduced with a `given` clause.
19
19
The `max` method can be applied as follows:
20
20
```scala
21
-
max(2, 3)givenIntOrd
21
+
max(2, 3)(givenIntOrd)
22
22
```
23
-
The `given IntOrd` part passes `IntOrd` as an argument for the `ord` parameter. But the point of
23
+
The `(given IntOrd)` part passes `IntOrd` as an argument for the `ord` parameter. But the point of
24
24
implicit parameters is that this argument can also be left out (and it usually is). So the following
25
25
applications are equally valid:
26
26
```scala
@@ -35,65 +35,68 @@ mentioned explicitly at all, since it is used only in synthesized arguments for
35
35
other implicit parameters. In that case one can avoid defining a parameter name
36
36
and just provide its type. Example:
37
37
```scala
38
-
defmaximum[T](xs: List[T])givenOrd[T]:T=
38
+
defmaximum[T](xs: List[T])(givenOrd[T]):T=
39
39
xs.reduceLeft(max)
40
40
```
41
41
`maximum` takes an implicit parameter of type `Ord` only to pass it on as an
42
42
inferred argument to `max`. The name of the parameter is left out.
43
43
44
-
Generally, implicit parameters may be defined either as a parameter list `(p_1: T_1, ..., p_n: T_n)`
45
-
or as a sequence of types, separated by commas.
44
+
Generally, implicit parameters may be defined either as a full parameter list `(given p_1: T_1, ..., p_n: T_n)` or just as a sequence of types `(given T_1, ..., T_n)`.
45
+
Vararg given parameters are not allowed.
46
46
47
47
## Inferring Complex Arguments
48
48
49
49
Here are two other methods that have an implicit parameter of type `Ord[T]`:
0 commit comments