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
Have (mostly) standard reference pages describing the new features.
The old proposal-style content is kept in the `discussion` subdirectory.
Changes compared to previous version
- anonymous context parameter syntax
- implicit as a modifier
- Conversion trait instead of ImplicitConversion
Copy file name to clipboardExpand all lines: docs/docs/reference/instances/context-params.md
+24-66Lines changed: 24 additions & 66 deletions
Original file line number
Diff line number
Diff line change
@@ -3,32 +3,36 @@ layout: doc-page
3
3
title: "Context Parameters and Arguments"
4
4
---
5
5
6
-
This page presents new syntax for defining implicit parameters that aligns definition and call syntax. In both cases, the implicit parameter or argument now follows a `with` connective.
7
-
On the definition side, the old syntax
6
+
Context parameters are the name of a new syntax for implicit parameters that aligns definition and call syntax. Parameter definitions
7
+
and method arguments both follow a `with` connective. On the definition side, the old syntax
8
8
```scala
9
9
deff(a: A)(implicitb: B)
10
10
```
11
11
is now expressed as
12
12
```scala
13
13
deff(a: A) with (b: B)
14
14
```
15
+
or, leaving out the parameter name,
16
+
```scala
17
+
deff(a: A) withB
18
+
```
15
19
Implicit parameters defined with the new syntax are also called _context parameters_.
16
20
They come with a matching syntax for applications: explicit arguments for context parameters are also given after a `with`.
17
21
18
22
The following example shows shows three methods that each have a context parameter for `Ord[T]`.
19
23
```scala
20
-
defmaximum[T](xs: List[T]) with(cmp: Ord[T]):T=
24
+
defmaximum[T](xs: List[T]) withOrd[T]:T=
21
25
xs.reduceLeft((x, y) =>if (x < y) y else x)
22
26
23
27
defdescending[T] with (asc: Ord[T]):Ord[T] =newOrd[T] {
24
28
def (x: T) compareTo (y: T) = asc.compareTo(y)(x)
25
29
}
26
30
27
-
defminimum[T](xs: List[T]) with(cmp: Ord[T])=
31
+
defminimum[T](xs: List[T]) withOrd[T] =
28
32
maximum(xs) with descending
29
33
```
30
-
The `minimum` method's right hand side defines the explicit argument `descending`.
31
-
Explicit arguments for context parameters can be left out. For instance,
34
+
The `minimum` method's right hand side passes `descending` as an explicit argument to `maximum(xs)`.
35
+
But usually, explicit arguments for context parameters are be left out. For instance,
32
36
given `xs: List[Int]`, the following calls are all possible (and they all normalize to the last one:)
33
37
```scala
34
38
maximum(xs)
@@ -45,77 +49,31 @@ f with (a, b)
45
49
defgwith (xy: (A, B))
46
50
g with ((a, b))
47
51
```
48
-
49
-
## Implicit Function Types and Closures
50
-
51
-
Implicit function types are expressed using the new reserved operator `|=>`. Examples:
52
+
Unlike existing implicit parameters, context parameters can be freely mixed with normal parameter lists.
53
+
A context parameter may be followed by a normal parameter and _vice versa_. There can be several context parameter
54
+
lists in a definition. Example:
52
55
```scala
53
-
Context|=>T
54
-
A|=>B|=>T
55
-
(A, B) |=>T
56
-
(x: A, y: B) |=>T
57
-
```
58
-
The `|=>` syntax was chosen for its resemblance with a turnstile symbol `|-` which signifies context dependencies.
56
+
deffwith (u: Universe) (x: u.T) withContext= ...
59
57
60
-
The old syntax `implicit A => B` is no longer available.
61
-
Implicit function types are applied using `with`:
62
-
```scala
63
-
valf:A|=>B
64
-
vala:A
65
-
f with a // OK
66
-
f(a) // error: `f` does not take parameters
58
+
instance global forUniverse { typeT=String ... }
59
+
instance ctx forContext { ... }
67
60
```
68
-
Since application of regular function types and implicit function types different, implicit function types are no longer subtypes of regular function types.
69
-
70
-
The `|=>` syntax can also be used for closures. It turns the parameter bindings into implicit
71
-
parameters and makes the closure's type an implicit function type
61
+
Then the following calls are all valid (and normalize to the last one)
The old syntax `implicit (a: A) => B` now creates a closure of a regular function type `A => B` instead of an implicit function type `A |=> B`. This matches the types of implicit closures in Scala 2.x.
79
-
80
-
## Example
81
68
82
-
Implementing postconditions via `ensuring`:
83
-
```scala
84
-
objectPostConditions {
85
-
opaquetypeWrappedResult[T] =T
86
-
87
-
private instance WrappedResult {
88
-
defapply[T](x: T):WrappedResult[T] = x
89
-
def (x: WrappedResult[T]) unwrap[T]:T= x
90
-
}
91
-
92
-
defresult[T] with (wrapped: WrappedResult[T]):T= wrapped.unwrap
Here is the new syntax of parameters, arguments, and implicit function types seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html).
71
+
Here is the new syntax of parametersand arguments seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html).
This page presents new syntax for defining implicit parameters that aligns definition and call syntax. In both cases, the implicit parameter or argument now follows a `with` connective.
7
+
On the definition side, the old syntax
8
+
```scala
9
+
deff(a: A)(implicitb: B)
10
+
```
11
+
is now expressed as
12
+
```scala
13
+
deff(a: A) with (b: B)
14
+
```
15
+
Implicit parameters defined with the new syntax are also called _context parameters_.
16
+
They come with a matching syntax for applications: explicit arguments for context parameters are also given after a `with`.
17
+
18
+
The following example shows shows three methods that each have a context parameter for `Ord[T]`.
19
+
```scala
20
+
defmaximum[T](xs: List[T]) with (cmp: Ord[T]):T=
21
+
xs.reduceLeft((x, y) =>if (x < y) y else x)
22
+
23
+
defdescending[T] with (asc: Ord[T]):Ord[T] =newOrd[T] {
24
+
def (x: T) compareTo (y: T) = asc.compareTo(y)(x)
25
+
}
26
+
27
+
defminimum[T](xs: List[T]) with (cmp: Ord[T]) =
28
+
maximum(xs) with descending
29
+
```
30
+
The `minimum` method's right hand side defines the explicit argument `descending`.
31
+
Explicit arguments for context parameters can be left out. For instance,
32
+
given `xs: List[Int]`, the following calls are all possible (and they all normalize to the last one:)
33
+
```scala
34
+
maximum(xs)
35
+
maximum(xs) with descending
36
+
maximum(xs) with (descending withIntOrd)
37
+
```
38
+
Arguments for context parameters must be given using the `with` syntax. So the expression `maximum(xs)(descending)` would give a type error.
39
+
40
+
The `with` connective is treated like an infix operator with the same precedence as other operators that start with a letter. The expression following a `with` may also be an argument list consisting of several implicit arguments separated by commas. If a tuple should be passed as a single implicit argument (probably an uncommon case), it has to be put in a pair of extra parentheses:
41
+
```scala
42
+
deffwith (x: A, y: B)
43
+
f with (a, b)
44
+
45
+
defgwith (xy: (A, B))
46
+
g with ((a, b))
47
+
```
48
+
49
+
## Implicit Function Types and Closures
50
+
51
+
Implicit function types are expressed using the new reserved operator `|=>`. Examples:
52
+
```scala
53
+
Context|=>T
54
+
A|=>B|=>T
55
+
(A, B) |=>T
56
+
(x: A, y: B) |=>T
57
+
```
58
+
The `|=>` syntax was chosen for its resemblance with a turnstile symbol `|-` which signifies context dependencies.
59
+
60
+
The old syntax `implicit A => B` is no longer available.
61
+
Implicit function types are applied using `with`:
62
+
```scala
63
+
valf:A|=>B
64
+
vala:A
65
+
f with a // OK
66
+
f(a) // error: `f` does not take parameters
67
+
```
68
+
Since application of regular function types and implicit function types different, implicit function types are no longer subtypes of regular function types.
69
+
70
+
The `|=>` syntax can also be used for closures. It turns the parameter bindings into implicit
71
+
parameters and makes the closure's type an implicit function type
The old syntax `implicit (a: A) => B` now creates a closure of a regular function type `A => B` instead of an implicit function type `A |=> B`. This matches the types of implicit closures in Scala 2.x.
79
+
80
+
## Example
81
+
82
+
Implementing postconditions via `ensuring`:
83
+
```scala
84
+
objectPostConditions {
85
+
opaquetypeWrappedResult[T] =T
86
+
87
+
private instance WrappedResult {
88
+
defapply[T](x: T):WrappedResult[T] = x
89
+
def (x: WrappedResult[T]) unwrap[T]:T= x
90
+
}
91
+
92
+
defresult[T] with (wrapped: WrappedResult[T]):T= wrapped.unwrap
Here is the new syntax of parameters, arguments, and implicit function types seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html).
0 commit comments