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
An implicit conversion from type `S` to type `T` is defined by an implicit value which has function type `S => T`, or by an implicit method convertible to a value of that type.
In Scala 2, an implicit conversion from type `S` to type `T` is defined by an [implicit value]({% link _tour/implicit-parameters.md %}) which has function type `S => T`, or by an implicit method convertible to a value of that type.
16
+
{% endtab %}
17
+
{% tab 'Scala 3' %}
18
+
In Scala 3, an implicit conversion from type `S` to type `T` is defined by a [given instance]({% link _tour/implicit-parameters.md %}) which has type `scala.Conversion[S, T]`. For compatibility with Scala 2, they can also be defined by an implicit method (read more in the Scala 2 tab).
19
+
{% endtab %}
20
+
{% endtabs %}
14
21
15
22
Implicit conversions are applied in two situations:
16
23
17
-
* If an expression `e` is of type `S`, and `S` does not conform to the expression's expected type `T`.
18
-
* In a selection `e.m` with `e` of type `S`, if the selector `m` does not denote a member of `S`.
24
+
1. If an expression `e` is of type `S`, and `S` does not conform to the expression's expected type `T`.
25
+
2. In a selection `e.m` with `e` of type `S`, if the selector `m` does not denote a member of `S`.
19
26
20
-
In the first case, a conversion `c` is searched for which is applicable to `e` and whose result type conforms to `T`.
21
-
In the second case, a conversion `c` is searched for which is applicable to `e` and whose result contains a member named `m`.
27
+
In the first case, a conversion `c` is searched for, which is applicable to `e` and whose result type conforms to `T`.
22
28
23
-
If an implicit method `List[A] => Ordered[List[A]]` is in scope, as well as an implicit method `Int => Ordered[Int]`, the following operation on the two lists of type `List[Int]` is legal:
29
+
An example is to pass a `scala.Int`, e.g. `x`, to a method that expects `scala.Long`. In this case, the implicit conversion `Int.int2long(x)` is inserted.
24
30
25
-
```
26
-
List(1, 2, 3) <= List(4, 5)
27
-
```
28
31
29
-
An implicit method `Int => Ordered[Int]` is provided automatically through `scala.Predef.intWrapper`. An example of an implicit method `List[A] => Ordered[List[A]]` is provided below.
32
+
In the second case, a conversion `c` is searched for, which is applicable to `e` and whose result contains a member named `m`.
30
33
31
-
```scala mdoc
32
-
importscala.language.implicitConversions
34
+
An example is to compare two strings `"foo" < "bar"`. In this case, `String` has no member `<`, so the implicit conversion `Predef.augmentString("foo") < "bar"` is inserted. (`scala.Predef` is automatically imported into all Scala programs.)
The implicitly imported object `scala.Predef` declares several aliases to frequently used types (e.g. `scala.collection.immutable.Map` is aliased to `Map`) and methods (e.g. `assert`) but also several implicit conversions.
43
-
44
-
For example, when calling a Java method that expects a `java.lang.Integer`, you are free to pass it a `scala.Int` instead. That's because Predef includes the following implicit conversions:
0 commit comments