Skip to content

Commit 1fdea9f

Browse files
committed
update tour/implicit-conversions for Scala 3
1 parent d2c43ef commit 1fdea9f

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

_tour/implicit-conversions.md

Lines changed: 30 additions & 29 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]`, or by an implicit method which can be eta-expanded to the function type `S => T`.
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

2027
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`.
22-
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:
24-
25-
```
26-
List(1, 2, 3) <= List(4, 5)
27-
```
28-
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.
30-
31-
```scala mdoc
32-
import scala.language.implicitConversions
3328

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

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

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:
32+
In the second case, a conversion `c` is searched for which is applicable to `e` and whose result contains a member named `m`.
4533

46-
```scala mdoc
47-
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.
4835

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 the implicit conversion definition (for Scala 2 style conversions).
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+
- the implicit conversion definition (for Scala 2 style conversions)
58+
- the call site (when an inserted conversion was defined by a given instance of `scala.Conversion`)
59+
- Invoke the compiler with `-language:implicitConversions`
60+
{% endtab %}
61+
{% endtabs %}

0 commit comments

Comments
 (0)