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 method can have an _implicit_ parameter list, marked by the _implicit_ keyword at the start of the parameter list. If the parameters in that parameter list are not passed as usual, Scala will look if it can get an implicit value of the correct type, and if it can, pass it automatically.
13
+
A method can have *contextual parameters*, also called *implicit parameters*, or more concisely *implicits*.
14
+
Parameter lists starting with the keyword `using` (or `implicit` in Scala 2) mark contextual parameters.
15
+
Unless the call site explicitly provides arguments for those parameters, Scala will look for implicitly available `given` (or `implicit` in Scala 2) values of the correct type.
16
+
If it can find appropriate values, it automatically passes them.
14
17
15
-
The places Scala will look for these parameters fall into two categories:
18
+
This is best shown using a small example first.
19
+
We define an interface `Comparator[A]` that can compare elements of type `A`, and provide two implementations, for `Int`s and `String`s.
20
+
We then define a method `max[A](x: A, y: A)` that returns the greater of the two arguments.
21
+
Since `x` and `y` are generically typed, in general we do not know how to compare them, but we can ask for an appropriate comparator.
22
+
As there is typically a canonical comparator for any given type `A`, we can declare them as *given*s, or *implicitly* available.
16
23
17
-
* Scala will first look for implicit definitions and implicit parameters that can be accessed directly (without a prefix) at the point the method with the implicit parameter block is called.
18
-
* Then it looks for members marked implicit in all the companion objects associated with the implicit candidate type.
19
-
20
-
A more detailed guide to where Scala looks for implicits can be found in [the FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html).
21
-
22
-
In the following example we define a method `sum` which computes the sum of a list of elements using the monoid's `add` and `unit` operations. Please note that implicit values cannot be top-level.
`Monoid` defines an operation called `add` here, that combines a pair of `A`s and returns another `A`, together with an operation called `unit` that is able to create some (specific) `A`.
42
+
defmax[A](x: A, y: A)(implicitcomparator: Comparator[A]):A=
43
+
if (comparator.compare(x, y) >=0) x
44
+
else y
45
+
46
+
println(max(10, 6)) // 10
47
+
println(max("hello", "world")) // world
48
+
```
53
49
54
-
To show how implicit parameters work, we first define monoids `stringMonoid` and `intMonoid` for strings and integers, respectively. The `implicit` keyword indicates that the corresponding object can be used implicitly.
50
+
```scala mdoc:fail
51
+
// does not compile:
52
+
println(max(false, true))
53
+
// ^
54
+
// error: could not find implicit value for parameter comparator: Comparator[Boolean]
55
+
```
55
56
56
-
The method `sum` takes a `List[A]` and returns an `A`, which takes the initial `A` from `unit`, and combines each next `A` in the list to that with the `add` method. Making the parameter `m` implicit here means we only have to provide the `xs` parameter when we call the method if Scala can find an implicit `Monoid[A]` to use for the implicit `m` parameter.
57
+
The `comparator` parameter is automatically filled in with `Comparator.IntComparator` for `max(10, 6)`, and with `Comparator.StringComparator` for `max("hello", "world")`.
58
+
Since no implicit `Comparator[Boolean]` can be found, the call `max(false, true)` fails to compile.
59
+
{% endtab %}
57
60
58
-
In our `main` method we call `sum` twice, and only provide the `xs` parameter. Scala will now look for an implicit in the scope mentioned above. The first call to `sum` passes a `List[Int]` for `xs`, which means that `A` is `Int`. The implicit parameter list with `m` is left out, so Scala will look for an implicit of type `Monoid[Int]`. The first lookup rule reads
61
+
{% tab 'Scala 3' for=implicits-comparator %}
62
+
```scala
63
+
traitComparator[A]:
64
+
defcompare(x: A, y: A):Int
59
65
60
-
> Scala will first look for implicit definitions and implicit parameters that can be accessed directly (without a prefix) at the point the method with the implicit parameter block is called.
`intMonoid` is an implicit definition that can be accessed directly in `main`. It is also of the correct type, so it's passed to the `sum` method automatically.
The second call to `sum` passes a `List[String]`, which means that `A` is `String`. Implicit lookup will go the same way as with `Int`, but will this time find `stringMonoid`, and pass that automatically as `m`.
74
+
defmax[A](x: A, y: A)(usingcomparator: Comparator[A]):A=
|no giveninstance of typeComparator[Boolean] was found for parameter comparator of method max
70
89
```
90
+
91
+
The `comparator` parameter is automatically filled in with the `given Comparator[Int]` for `max(10, 6)`, and with the `given Comparator[String]` for `max("hello", "world")`.
92
+
Since no `given Comparator[Boolean]` can be found, the call `max(false, true)` fails to compile.
93
+
{% endtab %}
94
+
95
+
{% endtabs %}
96
+
97
+
Scala will look for available given values in two places:
98
+
99
+
* Scala will first look for given definitions and using parameters that can be accessed directly (without a prefix) at the call site of `max`.
100
+
* Then it looks for members marked `given`/`implicit` in the companion objects associated with the implicit candidate type (for example: `object Comparator` for the candidate type `Comparator[Int]`).
101
+
102
+
A more detailed guide to where Scala looks for implicits can be found in [the FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html).
0 commit comments