@@ -27,25 +27,31 @@ The Scalafix rule named `ExplicitImplicitTypes` in [ohze/scala-rewrites](https:/
27
27
28
28
Scala 3 does not support implicit conversion from an implicit function value, of the form ` implicit val ev: A => B ` .
29
29
30
- The following piece of code is now invalid:
30
+ {% tabs scala-2-implicit_1 %}
31
+ {% tab 'Scala 2 Only' %}
31
32
32
- ``` scala
33
+ The following piece of code is now invalid in Scala 3:
34
+ ~~~ scala
33
35
trait Pretty {
34
36
val print : String
35
37
}
36
38
37
39
def pretty [A ](a : A )(implicit ev : A => Pretty ): String =
38
- a.print // Error: value print is not a member of A
39
- ```
40
+ a.print // In Scala 3, Error: value print is not a member of A
41
+ ~~~
42
+ {% endtab %}
43
+ {% endtabs %}
40
44
41
45
The [ Scala 3 migration compilation] ( tooling-migration-mode.html ) can warn you about those cases, but it does not try to fix it.
42
46
43
47
Be aware that this incompatibility can produce a runtime incompatibility and break your program.
44
48
Indeed the compiler can find another implicit conversion from a broader scope, which would eventually cause an undesired behavior at runtime.
45
49
46
- This example illustrates the case:
50
+ {% tabs shared-implicit_2 %}
51
+ {% tab 'Scala 2 and 3' %}
47
52
48
- ``` scala
53
+ This example illustrates the case:
54
+ ~~~ scala
49
55
trait Pretty {
50
56
val print : String
51
57
}
@@ -54,13 +60,15 @@ implicit def anyPretty(any: Any): Pretty = new Pretty { val print = "any" }
54
60
55
61
def pretty [A ](a : A )(implicit ev : A => Pretty ): String =
56
62
a.print // always print "any"
57
- ```
63
+ ~~~
64
+ {% endtab %}
65
+ {% endtabs %}
58
66
59
67
The resolved conversion depends on the compiler mode:
60
68
- ` -source:3.0-migration ` : the compiler performs the ` ev ` conversion
61
69
- ` -source:3.0 ` : the compiler cannot perform the ` ev ` conversion but it can perform the ` anyPretty ` , which is undesired
62
70
63
- One simple fix is to supply the right conversion explicitly:
71
+ In Scala 3, one simple fix is to supply the right conversion explicitly:
64
72
65
73
{% highlight diff %}
66
74
def pretty[ A] (a: A)(implicit ev: A => Pretty): String =
@@ -73,11 +81,15 @@ def pretty[A](a: A)(implicit ev: A => Pretty): String =
73
81
View bounds have been deprecated for a long time but they are still supported in Scala 2.13.
74
82
They cannot be compiled with Scala 3 anymore.
75
83
76
- ``` scala
84
+ {% tabs scala-2-bounds_1 %}
85
+ {% tab 'Scala 2 Only' %}
86
+ ~~~ scala
77
87
def foo [A <% Long ](a : A ): Long = a
78
- ```
88
+ ~~~
89
+ {% endtab %}
90
+ {% endtabs %}
79
91
80
- In this example we get:
92
+ In this example, in Scala 3, we get this following error message :
81
93
82
94
{% highlight text %}
83
95
-- Error: src/main/scala/view-bound.scala:2:12
@@ -104,12 +116,16 @@ It is not the case in Scala 3 anymore, and leads to an ambiguous conversion.
104
116
105
117
For instance, in this example:
106
118
107
- ``` scala
119
+ {% tabs scala-2-ambiguous_1 %}
120
+ {% tab 'Scala 2 Only' %}
121
+ ~~~ scala
108
122
implicit def boolFoo (bool : Boolean ): Foo = ???
109
123
implicit def lazyBoolFoo (lazyBool : => Boolean ): Foo = ???
110
124
111
125
true .foo()
112
- ```
126
+ ~~~
127
+ {% endtab %}
128
+ {% endtabs %}
113
129
114
130
The Scala 2.13 compiler chooses the ` boolFoo ` conversion but the Scala 3 compiler fails to compile.
115
131
@@ -131,4 +147,4 @@ implicit def lazyBoolFoo(lazyBool: => Boolean): Foo = ???
131
147
132
148
-true.foo()
133
149
+boolFoo(true).foo()
134
- {% endhighlight %}
150
+ {% endhighlight %}
0 commit comments