Skip to content

Commit b45c4c5

Browse files
authored
Merge pull request #2630 from bishabosha/tour-implicit-conversions
update tour/implicit-conversions for Scala 3
2 parents d2c43ef + b679db2 commit b45c4c5

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

_tour/implicit-conversions.md

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,33 @@ previous-page: implicit-parameters
1010
redirect_from: "/tutorials/tour/implicit-conversions.html"
1111
---
1212

13-
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.
13+
{% tabs implicit-conversion-defn class=tabs-scala-version %}
14+
{% tab 'Scala 2' %}
15+
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 %}
1421

1522
Implicit conversions are applied in two situations:
1623

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

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

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

25-
```
26-
List(1, 2, 3) <= List(4, 5)
27-
```
2831

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

31-
```scala mdoc
32-
import scala.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.)
3335

34-
implicit def list2ordered[A](x: List[A])
35-
(implicit elem2ordered: A => Ordered[A]): Ordered[List[A]] =
36-
new Ordered[List[A]] {
37-
//replace with a more useful implementation
38-
def compare(that: List[A]): Int = 1
39-
}
40-
```
41-
42-
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:
45-
46-
```scala mdoc
47-
import scala.language.implicitConversions
48-
49-
implicit def int2Integer(x: Int) =
50-
java.lang.Integer.valueOf(x)
51-
```
36+
**Beware the power of implicit conversions:**
5237

38+
{% tabs implicit-conversion-warning class=tabs-scala-version %}
39+
{% tab 'Scala 2' %}
5340
Because implicit conversions can have pitfalls if used indiscriminately the compiler warns when compiling the implicit conversion definition.
5441

5542
To turn off the warnings take either of these actions:
@@ -58,3 +45,17 @@ To turn off the warnings take either of these actions:
5845
* Invoke the compiler with `-language:implicitConversions`
5946

6047
No warning is emitted when the conversion is applied by the compiler.
48+
{% endtab %}
49+
{% tab 'Scala 3' %}
50+
Because implicit conversions can have pitfalls if used indiscriminately the compiler warns in two situations:
51+
- when compiling a Scala 2 style implicit conversion definition.
52+
- at the call site where a given instance of `scala.Conversion` is inserted as a conversion.
53+
54+
To turn off the warnings take either of these actions:
55+
56+
- Import `scala.language.implicitConversions` into the scope of:
57+
- a Scala 2 style implicit conversion definition
58+
- call sites where a given instance of `scala.Conversion` is inserted as a conversion.
59+
- Invoke the compiler with `-language:implicitConversions`
60+
{% endtab %}
61+
{% endtabs %}

0 commit comments

Comments
 (0)