|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Implicit Conversions - More Details" |
| 4 | +--- |
| 5 | + |
| 6 | +## Implementation |
| 7 | + |
| 8 | +An implicit conversion, or _view_, from type `S` to type `T` is |
| 9 | +defined by either: |
| 10 | + |
| 11 | +- An `implicit def` which has type `S => T` or `(=> S) => T` |
| 12 | +- An implicit value which has type `ImplicitConverter[S, T]` |
| 13 | + |
| 14 | +The standard library defines an abstract class `ImplicitConverter`: |
| 15 | + |
| 16 | +```scala |
| 17 | +abstract class ImplicitConverter[-T, +U] extends Function1[T, U] |
| 18 | +``` |
| 19 | + |
| 20 | +Function literals are automatically converted to `ImplicitConverter` |
| 21 | +values. |
| 22 | + |
| 23 | +Views are applied in three situations: |
| 24 | + |
| 25 | +1. If an expression `e` is of type `T`, and `T` does not conform to |
| 26 | + the expression's expected type `pt`. In this case, an implicit `v` |
| 27 | + which is applicable to `e` and whose result type conforms to `pt` |
| 28 | + is searched. The search proceeds as in the case of implicit |
| 29 | + parameters, where the implicit scope is the one of `T => pt`. If |
| 30 | + such a view is found, the expression `e` is converted to `v(e)`. |
| 31 | +1. In a selection `e.m` with `e` of type `T`, if the selector `m` does |
| 32 | + not denote an accessible member of `T`. In this case, a view `v` |
| 33 | + which is applicable to `e` and whose result contains an accessible |
| 34 | + member named `m` is searched. The search proceeds as in the case of |
| 35 | + implicit parameters, where the implicit scope is the one of `T`. If |
| 36 | + such a view is found, the selection `e.m` is converted to `v(e).m`. |
| 37 | +1. In an application `e.m(args)` with `e` of type `T`, if the selector |
| 38 | + `m` denotes some accessible member(s) of `T`, but none of these |
| 39 | + members is applicable to the arguments `args`. In this case, a view |
| 40 | + `v` which is applicable to `e` and whose result contains a method |
| 41 | + `m` which is applicable to `args` is searched. The search proceeds |
| 42 | + as in the case of implicit parameters, where the implicit scope is |
| 43 | + the one of `T`. If such a view is found, the application |
| 44 | + `e.m(args)` is converted to `v(e).m(args)`. |
| 45 | + |
| 46 | +# Differences with Scala 2 implicit conversions |
| 47 | + |
| 48 | +In Scala 2, views whose parameters are passed by-value take precedence |
| 49 | +over views whose parameters are passed by-name. This is no longer the |
| 50 | +case in Scala 3. A type error reporting the ambiguous conversions will |
| 51 | +be emitted in cases where this rule would be applied in Scala 2: |
| 52 | + |
| 53 | +```scala |
| 54 | +implicit def conv1(x: Int): String = x.toString |
| 55 | +implicit def conv2(x: => Int): String = x.toString |
| 56 | + |
| 57 | +val x: String = 0 // Compiles in Scala2 (uses `conv1`), |
| 58 | + // type error in Scala 3 because of ambiguity. |
| 59 | +``` |
| 60 | + |
| 61 | +In Scala 2, implicit values of a function type would be considered as |
| 62 | +potential views. In Scala 3, these implicit value need to have type |
| 63 | +`ImplicitConverter`: |
| 64 | + |
| 65 | +```scala |
| 66 | +// Scala 2: |
| 67 | +def foo(x: Int)(implicit conv: Int => String): String = x |
| 68 | + |
| 69 | +// Becomes with Scala 3: |
| 70 | +def foo(x: Int)(implicit conv: ImplicitConverter[Int, String]): String = x |
| 71 | + |
| 72 | +// Call site is unchanged: |
| 73 | +foo(4)(_.toString) |
| 74 | + |
| 75 | +// Scala 2: |
| 76 | +implicit val myConverter: Int => String = _.toString |
| 77 | + |
| 78 | +// Becomes with Scala 3: |
| 79 | +implicit val myConverter: ImplicitConverter[Int, String] = _.toString |
| 80 | +``` |
| 81 | + |
| 82 | +Note that implicit conversions are also affected by the [changes to |
| 83 | +implicit resolution](implicit-resolution.html) between Scala 2 and |
| 84 | +Scala 3. |
| 85 | + |
| 86 | +## Motivation for the changes |
| 87 | + |
| 88 | +The introduction of `ImplicitConverter` in Scala 3 and the decision to |
| 89 | +restrict implicit values of this type to be considered as potential |
| 90 | +views comes from the desire to remove surprising behavior from the |
| 91 | +language: |
| 92 | + |
| 93 | +```scala |
| 94 | +implicit val m: Map[Int, String] = Map(1 -> "abc") |
| 95 | + |
| 96 | +val x: String = 1 // scalac: assigns "abc" to x |
| 97 | + // Dotty: type error |
| 98 | +``` |
| 99 | + |
| 100 | +This snippet contains a type error. The right hand side of `val x` |
| 101 | +does not conform to type `String`. In Scala 2, the compiler will use |
| 102 | +`m` as an implicit conversion from `Int` to `String`, whereas Scala 3 |
| 103 | +will report a type error, because Map isn't an instance of |
| 104 | +`ImplicitConverter`. |
| 105 | + |
| 106 | +## Migration path |
| 107 | + |
| 108 | +Implicit values that are used as views should see their type changed |
| 109 | +to `ImplicitConverter`. |
| 110 | + |
| 111 | +For the migration of implicit conversions that are affected by the |
| 112 | +changes to implicit resolution, refer to the [Changes in Implicit |
| 113 | +Resolution](implicit-resolution.html) for more information. |
| 114 | + |
| 115 | +## Reference |
| 116 | + |
| 117 | +For more information about implicit resolution, see [Changes in |
| 118 | +Implicit Resolution](implicit-resolution.html). |
| 119 | +Other details are available in |
| 120 | +[PR #2065](https://github.com/lampepfl/dotty/pull/2065) |
| 121 | + |
0 commit comments