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
A special form of import is used to import evidence values. Example:
6
+
A special form of import is used to import implicit values. Example:
7
7
```scala
8
8
objectA {
9
9
classTC
10
-
evidence tc forTC
10
+
implicit tc forTC
11
11
deffgivenTC=???
12
12
}
13
13
objectB {
14
14
importA._
15
-
importevidenceA._
15
+
importimplicitA._
16
16
}
17
17
```
18
18
In the code above, the `import A._` clause of object `B` will import all members
19
-
of `A`_except_ the evidence`tc`. Conversely, the second import `import evidence A._` will import _only_ that evidence.
19
+
of `A`_except_ the implicit`tc`. Conversely, the second import `import implicit A._` will import _only_ that implicit.
20
20
21
-
Generally, a normal import clause brings all members except evidence values into scope whereas an `import evidence` clause brings only evidence values into scope.
21
+
Generally, a normal import clause brings all members except implicit values into scope whereas an `import implicit` clause brings only implicit values into scope.
22
22
23
23
There are two main benefits arising from these rules:
24
24
25
-
- It is made clearer where evidence values in scope are coming from. In particular, it is not possible to hide imported evidence values in a long list of regular imports.
26
-
- It enables importing all evidence values
27
-
without importing anything else. This is particularly important since evidence
25
+
- It is made clearer where implicit values in scope are coming from. In particular, it is not possible to hide imported implicit values in a long list of regular imports.
26
+
- It enables importing all implicit values
27
+
without importing anything else. This is particularly important since implicit
28
28
values can be anonymous, so the usual recourse of using named imports is not
29
29
practical.
30
30
31
31
### Relationship with Old-Style Implicits
32
32
33
-
The rules of evidence imports above have the consequence that a library
33
+
The rules of "import implicit" above have the consequence that a library
34
34
would have to migrate in lockstep with all its users from old style implicit definitions and
35
-
normal imports to evidence definitions and evidence imports.
35
+
normal imports to new style implicit definitions and `import implicit`.
36
36
37
37
The following modifications avoid this hurdle to migration.
38
38
39
-
1. An evidence import also brings old style implicits into scope. So, in Scala 3.0
40
-
an old-style implicit definition can be brought into scope either by a normal or
41
-
by an evidence import.
39
+
1. An `import implicit` also brings old style implicits into scope. So, in Scala 3.0
40
+
an old-style implicit definition can be brought into scope either by a normal import
41
+
or by an `import implicit`.
42
42
43
43
2. In Scala 3.1, an old-style implicits accessed implicitly through a normal import
44
44
will give a deprecation warning.
45
45
46
46
3. In some version after 3.1, an old-style implicits accessed implicitly through a normal import
47
47
will give a compiler error.
48
48
49
-
These rules mean that library users can use `import evidence` to access old-style implicits in Scala 3.0,
49
+
These rules mean that library users can use `import implicit` to access old-style implicits in Scala 3.0,
50
50
and will be gently nudged and then forced to do so in later versions. Libraries can then switch to
51
-
evidence definitions once their user base has migrated.
51
+
new-style implicit definitions once their user base has migrated.
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual-implicit/inferable-by-name-parameters.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,9 @@ trait Codec[T] {
10
10
defwrite(x: T):Unit
11
11
}
12
12
13
-
evidence intCodec forCodec[Int] =???
13
+
implicit intCodec forCodec[Int] =???
14
14
15
-
evidence optionCodec[T] given (ev: =>Codec[T]) forCodec[Option[T]] {
15
+
implicit optionCodec[T] given (ev: =>Codec[T]) forCodec[Option[T]] {
16
16
defwrite(xo: Option[T]) = xo match {
17
17
caseSome(x) => ev.write(x)
18
18
caseNone=>
@@ -33,20 +33,20 @@ if this is necessary to prevent an otherwise diverging expansion.
33
33
34
34
The precise steps for constructing an inferable argument for a by-name parameter of type `=> T` are as follows.
35
35
36
-
1. Create a new evidence value of type `T`:
36
+
1. Create a new implicit value of type `T`:
37
37
38
38
```scala
39
-
evidence lv forT=???
39
+
implicit lv forT=???
40
40
```
41
41
where `lv` is an arbitrary fresh name.
42
42
43
43
1. This instance is not immediately available ascandidatefor 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 inferred argument to a by-name parameter.
44
44
45
-
1. Ifthis search succeeds with expression `E`, and `E` contains references to the evidence `lv`, replace `E` by
45
+
1. Ifthis search succeeds with expression `E`, and `E` contains references to the implicit `lv`, replace `E` by
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual-implicit/inferable-params.md
+11-11Lines changed: 11 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -9,13 +9,13 @@ call trees where the same value is passed over and over again in long call chain
9
9
functions. Given clauses can help here since they enable the compiler to synthesize
10
10
repetitive arguments instead of the programmer having to write them explicitly.
11
11
12
-
For example, given the [evidence definitions](./instance-defs.md) of the previous section,
12
+
For example, given the [implicit instances](./instance-defs.md) of the previous section,
13
13
a maximum function that works for any arguments for which an ordering exists can be defined as follows:
14
14
```scala
15
15
defmax[T](x: T, y: T) given (ord: Ord[T]):T=
16
16
if (ord.compare(x, y) <1) y else x
17
17
```
18
-
Here, the part following `given` introduces a constraint that `T` is ordered, or, otherwise put, that evidence for `Ord[T]` exists. The evidence is passed as an _implicit parameter_ to the method. Inside the method, the evidence value can be accessed under the name `ord`.
18
+
Here, the part following `given` introduces a constraint that `T` is ordered, or, otherwise put, that evidence for `Ord[T]` exists. The evidence is passed as an _implicit parameter_ to the method. Inside the method, the implicit value can be accessed under the name `ord`.
19
19
20
20
The `max` method can be applied as follows:
21
21
```scala
@@ -29,14 +29,14 @@ max(List(1, 2, 3), Nil)
29
29
30
30
## Anonymous Inferable Parameters
31
31
32
-
In many situations, the name of an evidence parameter of a method need not be mentioned explicitly at all, since it is only used as synthesized evidence for other constraints. In that case one can avoid defining a parameter name and just provide its type. Example:
32
+
In many situations, the name of an implicit parameter of a method need not be mentioned explicitly at all, since it is only used as synthesized implicit for other constraints. In that case one can avoid defining a parameter name and just provide its type. Example:
33
33
```scala
34
34
defmaximum[T](xs: List[T]) givenOrd[T]:T=
35
35
xs.reduceLeft(max)
36
36
```
37
-
`maximum` takes an evidence parameter of type `Ord` only to pass it on as an implicit argument to `max`. The name of the parameter is left out.
37
+
`maximum` takes an implicit parameter of type `Ord` only to pass it on as an implicit argument to `max`. The name of the parameter is left out.
38
38
39
-
Generally, evidence parameters may be given either as a parameter list `(p_1: T_1, ..., p_n: T_n)` or as a sequence of types, separated by commas.
39
+
Generally, implicit parameters may be given either as a parameter list `(p_1: T_1, ..., p_n: T_n)` or as a sequence of types, separated by commas.
40
40
41
41
## Inferring Complex Arguments
42
42
@@ -66,8 +66,8 @@ There can be several inferable parameter lists in a definition. Example:
0 commit comments