Skip to content

Commit 345a6d3

Browse files
authored
Merge pull request #6378 from dotty-staging/contextual-implicit
More fixes to contextual-implicit
2 parents 22ba798 + 0182028 commit 345a6d3

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
---
22
layout: doc-page
3-
title: "Evidence Imports"
3+
title: "Import Implicit"
44
---
55

6-
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:
77
```scala
88
object A {
99
class TC
10-
evidence tc for TC
10+
implicit tc for TC
1111
def f given TC = ???
1212
}
1313
object B {
1414
import A._
15-
import evidence A._
15+
import implicit A._
1616
}
1717
```
1818
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.
2020

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.
2222

2323
There are two main benefits arising from these rules:
2424

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
2828
values can be anonymous, so the usual recourse of using named imports is not
2929
practical.
3030

3131
### Relationship with Old-Style Implicits
3232

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
3434
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`.
3636

3737
The following modifications avoid this hurdle to migration.
3838

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`.
4242

4343
2. In Scala 3.1, an old-style implicits accessed implicitly through a normal import
4444
will give a deprecation warning.
4545

4646
3. In some version after 3.1, an old-style implicits accessed implicitly through a normal import
4747
will give a compiler error.
4848

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,
5050
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.

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

Lines changed: 6 additions & 6 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-
evidence intCodec for Codec[Int] = ???
13+
implicit intCodec for Codec[Int] = ???
1414

15-
evidence optionCodec[T] given (ev: => Codec[T]) for Codec[Option[T]] {
15+
implicit optionCodec[T] given (ev: => Codec[T]) for Codec[Option[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 constructing an inferable argument for a by-name parameter of type `=> T` are as follows.
3535

36-
1. Create a new evidence value of type `T`:
36+
1. Create a new implicit value of type `T`:
3737

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

4343
1. This 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 inferred argument to a by-name parameter.
4444

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

4747

4848
```scala
49-
{ evidence lv for T = E; lv }
49+
{ implicit lv for T = E; lv }
5050
```
5151

5252
Otherwise, return `E` unchanged.

docs/docs/reference/contextual-implicit/inferable-params.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ 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 [evidence definitions](./instance-defs.md) of the previous section,
12+
For example, given the [implicit instances](./instance-defs.md) of the previous section,
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 =
1616
if (ord.compare(x, y) < 1) y else x
1717
```
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`.
1919

2020
The `max` method can be applied as follows:
2121
```scala
@@ -29,14 +29,14 @@ max(List(1, 2, 3), Nil)
2929

3030
## Anonymous Inferable Parameters
3131

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:
3333
```scala
3434
def maximum[T](xs: List[T]) given Ord[T]: T =
3535
xs.reduceLeft(max)
3636
```
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.
3838

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.
4040

4141
## Inferring Complex Arguments
4242

@@ -66,8 +66,8 @@ There can be several inferable parameter lists in a definition. Example:
6666
```scala
6767
def f given (u: Universe) (x: u.T) given Context = ...
6868

69-
evidence global for Universe { type T = String ... }
70-
evidence ctx for Context { ... }
69+
implicit global for Universe { type T = String ... }
70+
implicit ctx for Context { ... }
7171
```
7272
Then the following calls are all valid (and normalize to the last one)
7373
```scala
@@ -77,17 +77,17 @@ f("abc") given ctx
7777
(f given global)("abc") given ctx
7878
```
7979

80-
## Summoning the Evidence
80+
## Summoning an Implicit
8181

82-
A method `the` in `Predef` summons the evidence for a given type. For example, the evidence for `Ord[List[Int]]` is generated by
82+
A method `the` in `Predef` summons an implicit for a given type. For example, the implicit for `Ord[List[Int]]` is generated by
8383
```scala
8484
the[Ord[List[Int]]] // reduces to ListOrd given IntOrd
8585
```
86-
The `the` method is simply defined as the (non-widening) identity function over an evidence parameter.
86+
The `the` method is simply defined as the (non-widening) identity function over an implicit parameter.
8787
```scala
8888
def the[T] given (x: T): x.type = x
8989
```
90-
Functions like `the` that have only evidence parameters are also called _context queries_.
90+
Functions like `the` that have only implicit parameters are also called _context queries_.
9191

9292
## Syntax
9393

docs/docs/reference/contextual-implicit/typeclasses.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ layout: doc-page
33
title: "Implementing Typeclasses"
44
---
55

6-
Evidence definitions, extension methods and context bounds
6+
Implicit instances, extension methods and context bounds
77
allow a concise and natural expression of _typeclasses_. Typeclasses are just traits
8-
with canonical implementations defined by evidence definitions. Here are some examples of standard typeclasses:
8+
with canonical implementations defined by implicit instances. Here are some examples of standard typeclasses:
99

1010
### Semigroups and monoids:
1111

@@ -20,12 +20,12 @@ object Monoid {
2020
def apply[T] given Monoid[T] = the[Monoid[T]]
2121
}
2222

23-
evidence for Monoid[String] {
23+
implicit for Monoid[String] {
2424
def (x: String) combine (y: String): String = x.concat(y)
2525
def unit: String = ""
2626
}
2727

28-
evidence for Monoid[Int] {
28+
implicit for Monoid[Int] {
2929
def (x: Int) combine (y: Int): Int = x + y
3030
def unit: Int = 0
3131
}
@@ -48,14 +48,14 @@ trait Monad[F[_]] extends Functor[F] {
4848
def pure[A](x: A): F[A]
4949
}
5050

51-
evidence ListMonad for Monad[List] {
51+
implicit ListMonad for Monad[List] {
5252
def (xs: List[A]) flatMap [A, B] (f: A => List[B]): List[B] =
5353
xs.flatMap(f)
5454
def pure[A](x: A): List[A] =
5555
List(x)
5656
}
5757

58-
evidence ReaderMonad[Ctx] for Monad[[X] => Ctx => X] {
58+
implicit ReaderMonad[Ctx] for Monad[[X] => Ctx => X] {
5959
def (r: Ctx => A) flatMap [A, B] (f: A => Ctx => B): Ctx => B =
6060
ctx => f(r(ctx))(ctx)
6161
def pure[A](x: A): Ctx => A =

0 commit comments

Comments
 (0)