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
Copy file name to clipboardExpand all lines: _overviews/scala3-migration/incompat-dropped-features.md
+78-41
Original file line number
Diff line number
Diff line change
@@ -27,14 +27,17 @@ But the `scala.Symbol` class still exists so that each string literal can be saf
27
27
28
28
This piece of code cannot be compiled with Scala 3:
29
29
30
-
```scala
30
+
{% tabs scala-2-literals_1 %}
31
+
{% tab 'Scala 2 Only' %}
32
+
~~~scala
31
33
valvalues:Map[Symbol, Int] =Map('abc->1)
32
34
33
-
valabc= values('abc) // Migration Warning: symbol literal 'abc is no longer supported
34
-
```
35
+
valabc= values('abc) // In Scala 3, Migration Warning: symbol literal 'abc is no longer supported
36
+
~~~
37
+
{% endtab %}
38
+
{% endtabs %}
35
39
36
40
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into:
37
-
38
41
{% highlight diff %}
39
42
val values: Map[Symbol, Int] = Map(Symbol("abc") -> 1)
40
43
@@ -54,19 +57,26 @@ It is recommended to use the equivalent `while ({ <body>; <cond> }) ()` that can
54
57
55
58
The following piece of code cannot be compiled with Scala 3.
56
59
57
-
```scala
58
-
do { // Migration Warning: `do <body> while <cond>` is no longer supported
60
+
{% tabs scala-2-do_while_1 %}
61
+
{% tab 'Scala 2 Only' %}
62
+
~~~scala
63
+
do { // In Scala 3, Migration Warning: `do <body> while <cond>` is no longer supported
59
64
i +=1
60
65
} while (f(i) ==0)
61
-
```
66
+
~~~
67
+
{% endtab %}
68
+
{% endtabs %}
62
69
63
70
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites it into.
64
-
65
-
```scala
71
+
{% tabs scala-3-do_while_2 %}
72
+
{% tab 'Scala 3 Only' %}
73
+
~~~scala
66
74
while ({ {
67
75
i +=1
68
76
} ; f(i) ==0}) ()
69
-
```
77
+
~~~
78
+
{% endtab %}
79
+
{% endtabs %}
70
80
71
81
## Auto-application
72
82
@@ -75,16 +85,19 @@ It is deprecated in Scala 2.13 and dropped in Scala 3.
75
85
76
86
The following code is invalid in Scala 3:
77
87
78
-
```scala
88
+
{% tabs scala-2-auto_application_1 %}
89
+
{% tab 'Scala 2 Only' %}
90
+
~~~scala
79
91
objectHello {
80
92
defmessage():String="Hello"
81
93
}
82
94
83
-
println(Hello.message) // Migration Warning: method message must be called with () argument
84
-
```
95
+
println(Hello.message) // In Scala 3, Migration Warning: method message must be called with () argument
96
+
~~~
97
+
{% endtab %}
98
+
{% endtabs %}
85
99
86
100
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites it into:
87
-
88
101
{% highlight diff %}
89
102
object Hello {
90
103
def message(): String = "Hello"
@@ -103,13 +116,16 @@ Furthermore Scala 3 does not allow eta-expansion of values to nullary functions
103
116
104
117
Thus, this piece of code is invalid in Scala 3:
105
118
106
-
```scala
119
+
{% tabs scala-2-eta_expansion_1 %}
120
+
{% tab 'Scala 2 Only' %}
121
+
~~~scala
107
122
valx=1
108
-
valf: () =>Int= x _ // Migration Warning: The syntax `<function> _` is no longer supported;
109
-
```
123
+
valf: () =>Int= x _ // In Scala 3, Migration Warning: The syntax `<function> _` is no longer supported;
124
+
~~~
125
+
{% endtab %}
126
+
{% endtabs %}
110
127
111
128
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites it into:
112
-
113
129
{% highlight diff %}
114
130
val x = 1
115
131
-val f: () => Int = x _
@@ -120,14 +136,17 @@ val x = 1
120
136
121
137
The implicit `Predef.any2stringadd` conversion is deprecated in Scala 2.13 and dropped in Scala 3.
122
138
123
-
This piece of code does not compile anymore.
139
+
This piece of code does not compile anymore in Scala 3.
124
140
125
-
```scala
126
-
valstr=newAnyRef+"foo"// Error: value + is not a member of Object
127
-
```
141
+
{% tabs scala-2-any2stringadd_1 %}
142
+
{% tab 'Scala 2 Only' %}
143
+
~~~scala
144
+
valstr=newAnyRef+"foo"// In Scala 3, Error: value + is not a member of Object
145
+
~~~
146
+
{% endtab %}
147
+
{% endtabs %}
128
148
129
149
The conversion to `String` must be applied explicitly, for instance with `String.valueOf`.
130
-
131
150
{% highlight diff %}
132
151
-val str = new AnyRef + "foo"
133
152
+val str = String.valueOf(new AnyRef) + "foo"
@@ -140,9 +159,11 @@ This rewrite can be applied by the `fix.scala213.Any2StringAdd` Scalafix rule in
140
159
Early initializers are deprecated in Scala 2.13 and dropped in Scala 3.
141
160
They were rarely used, and mostly to compensate for the lack of [Trait parameters]({{ site.scala3ref }}/other-new-features/trait-parameters.html) which are now supported in Scala 3.
142
161
143
-
That is why the following piece of code does not compile anymore.
162
+
That is why the following piece of code does not compile anymore in Scala 3.
144
163
145
-
```scala
164
+
{% tabs scala-2-initializer_1 %}
165
+
{% tab 'Scala 2 Only' %}
166
+
~~~scala
146
167
traitBar {
147
168
valname:String
148
169
valsize:Int= name.size
@@ -151,7 +172,9 @@ trait Bar {
151
172
objectFooextends {
152
173
valname="Foo"
153
174
} withBar
154
-
```
175
+
~~~
176
+
{% endtab %}
177
+
{% endtabs %}
155
178
156
179
The Scala 3 compiler produces two error messages:
157
180
@@ -161,7 +184,6 @@ The Scala 3 compiler produces two error messages:
161
184
| ^
162
185
| `extends` must be followed by at least one parent
@@ -171,51 +193,66 @@ The Scala 3 compiler produces two error messages:
171
193
172
194
It suggests to use trait parameters which would give us:
173
195
174
-
```scala
196
+
{% tabs scala-3-initializer_2 %}
197
+
{% tab 'Scala 3 Only' %}
198
+
~~~scala
175
199
traitBar(name: String) {
176
200
valsize:Int= name.size
177
201
}
178
202
179
203
objectFooextendsBar("Foo")
180
-
```
204
+
~~~
205
+
{% endtab %}
206
+
{% endtabs %}
181
207
182
208
Since trait parameters are not available in Scala 2.13, it does not cross-compile.
183
209
If you need a cross-compiling solution you can use an intermediate class that carries the early initialized `val`s and `var`s as constructor parameters.
In the case of a class, it is also possible to use a secondary constructor with a fixed value, as shown by:
192
-
193
-
```scala
220
+
~~~scala
194
221
classFizzprivate (valname:String) extendsBar {
195
222
defthis() =this("Fizz")
196
223
}
197
-
```
224
+
~~~
225
+
{% endtab %}
226
+
{% endtabs %}
198
227
199
228
## Existential Type
200
229
201
230
Existential type is a [dropped feature]({{ site.scala3ref }}/dropped-features/existential-types.html), which makes the following code invalid.
202
-
203
-
```scala
204
-
deffoo:List[Class[T]] forSome { typeT } // Error: Existential types are no longer supported
205
-
```
231
+
232
+
{% tabs scala-2-existential_1 %}
233
+
{% tab 'Scala 2 Only' %}
234
+
~~~scala
235
+
deffoo:List[Class[T]] forSome { typeT } // In Scala 3, Error: Existential types are no longer supported
236
+
~~~
237
+
{% endtab %}
238
+
{% endtabs %}
206
239
207
240
> Existential type is an experimental feature in Scala 2.13 that must be enabled explicitly either by importing `import scala.language.existentials` or by setting the `-language:existentials` compiler flag.
208
241
209
-
The proposed solution is to introduce an enclosing type that carries the dependent type:
242
+
In Scala 3, the proposed solution is to introduce an enclosing type that carries the dependent type:
210
243
211
-
```scala
244
+
{% tabs shared-existential_1 %}
245
+
{% tab 'Scala 2 and 3' %}
246
+
~~~scala
212
247
traitBar {
213
248
typeT
214
249
valvalue:List[Class[T]]
215
250
}
216
251
217
252
deffoo:Bar
218
-
```
253
+
~~~
254
+
{% endtab %}
255
+
{% endtabs %}
219
256
220
257
Note that using a wildcard argument, `_` or `?`, is often simpler but is not always possible.
221
258
For instance you could replace `List[T] forSome { type T }` by `List[?]`.
0 commit comments