Skip to content

Commit 8e8e357

Browse files
authored
Add code tabs to the Scala 3 Dropped Features (#2723)
1 parent 0513751 commit 8e8e357

File tree

1 file changed

+78
-41
lines changed

1 file changed

+78
-41
lines changed

Diff for: _overviews/scala3-migration/incompat-dropped-features.md

+78-41
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ But the `scala.Symbol` class still exists so that each string literal can be saf
2727

2828
This piece of code cannot be compiled with Scala 3:
2929

30-
```scala
30+
{% tabs scala-2-literals_1 %}
31+
{% tab 'Scala 2 Only' %}
32+
~~~ scala
3133
val values: Map[Symbol, Int] = Map('abc -> 1)
3234

33-
val abc = values('abc) // Migration Warning: symbol literal 'abc is no longer supported
34-
```
35+
val abc = values('abc) // In Scala 3, Migration Warning: symbol literal 'abc is no longer supported
36+
~~~
37+
{% endtab %}
38+
{% endtabs %}
3539

3640
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into:
37-
3841
{% highlight diff %}
3942
val values: Map[Symbol, Int] = Map(Symbol("abc") -> 1)
4043

@@ -54,19 +57,26 @@ It is recommended to use the equivalent `while ({ <body>; <cond> }) ()` that can
5457

5558
The following piece of code cannot be compiled with Scala 3.
5659

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
5964
i += 1
6065
} while (f(i) == 0)
61-
```
66+
~~~
67+
{% endtab %}
68+
{% endtabs %}
6269

6370
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
6674
while ({ {
6775
i += 1
6876
} ; f(i) == 0}) ()
69-
```
77+
~~~
78+
{% endtab %}
79+
{% endtabs %}
7080

7181
## Auto-application
7282

@@ -75,16 +85,19 @@ It is deprecated in Scala 2.13 and dropped in Scala 3.
7585

7686
The following code is invalid in Scala 3:
7787

78-
```scala
88+
{% tabs scala-2-auto_application_1 %}
89+
{% tab 'Scala 2 Only' %}
90+
~~~ scala
7991
object Hello {
8092
def message(): String = "Hello"
8193
}
8294

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 %}
8599

86100
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites it into:
87-
88101
{% highlight diff %}
89102
object Hello {
90103
def message(): String = "Hello"
@@ -103,13 +116,16 @@ Furthermore Scala 3 does not allow eta-expansion of values to nullary functions
103116

104117
Thus, this piece of code is invalid in Scala 3:
105118

106-
```scala
119+
{% tabs scala-2-eta_expansion_1 %}
120+
{% tab 'Scala 2 Only' %}
121+
~~~ scala
107122
val x = 1
108-
val f: () => Int = x _ // Migration Warning: The syntax `<function> _` is no longer supported;
109-
```
123+
val f: () => Int = x _ // In Scala 3, Migration Warning: The syntax `<function> _` is no longer supported;
124+
~~~
125+
{% endtab %}
126+
{% endtabs %}
110127

111128
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites it into:
112-
113129
{% highlight diff %}
114130
val x = 1
115131
-val f: () => Int = x _
@@ -120,14 +136,17 @@ val x = 1
120136

121137
The implicit `Predef.any2stringadd` conversion is deprecated in Scala 2.13 and dropped in Scala 3.
122138

123-
This piece of code does not compile anymore.
139+
This piece of code does not compile anymore in Scala 3.
124140

125-
```scala
126-
val str = new AnyRef + "foo" // Error: value + is not a member of Object
127-
```
141+
{% tabs scala-2-any2stringadd_1 %}
142+
{% tab 'Scala 2 Only' %}
143+
~~~ scala
144+
val str = new AnyRef + "foo" // In Scala 3, Error: value + is not a member of Object
145+
~~~
146+
{% endtab %}
147+
{% endtabs %}
128148

129149
The conversion to `String` must be applied explicitly, for instance with `String.valueOf`.
130-
131150
{% highlight diff %}
132151
-val str = new AnyRef + "foo"
133152
+val str = String.valueOf(new AnyRef) + "foo"
@@ -140,9 +159,11 @@ This rewrite can be applied by the `fix.scala213.Any2StringAdd` Scalafix rule in
140159
Early initializers are deprecated in Scala 2.13 and dropped in Scala 3.
141160
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.
142161

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

145-
```scala
164+
{% tabs scala-2-initializer_1 %}
165+
{% tab 'Scala 2 Only' %}
166+
~~~ scala
146167
trait Bar {
147168
val name: String
148169
val size: Int = name.size
@@ -151,7 +172,9 @@ trait Bar {
151172
object Foo extends {
152173
val name = "Foo"
153174
} with Bar
154-
```
175+
~~~
176+
{% endtab %}
177+
{% endtabs %}
155178

156179
The Scala 3 compiler produces two error messages:
157180

@@ -161,7 +184,6 @@ The Scala 3 compiler produces two error messages:
161184
| ^
162185
| `extends` must be followed by at least one parent
163186
{% endhighlight %}
164-
165187
{% highlight text %}
166188
-- [E009] Syntax Error: src/main/scala/early-initializer.scala:8:2
167189
8 |} with Bar
@@ -171,51 +193,66 @@ The Scala 3 compiler produces two error messages:
171193

172194
It suggests to use trait parameters which would give us:
173195

174-
```scala
196+
{% tabs scala-3-initializer_2 %}
197+
{% tab 'Scala 3 Only' %}
198+
~~~ scala
175199
trait Bar(name: String) {
176200
val size: Int = name.size
177201
}
178202

179203
object Foo extends Bar("Foo")
180-
```
204+
~~~
205+
{% endtab %}
206+
{% endtabs %}
181207

182208
Since trait parameters are not available in Scala 2.13, it does not cross-compile.
183209
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.
184210

185-
```scala
211+
{% tabs shared-initializer_4 %}
212+
{% tab 'Scala 2 and 3' %}
213+
~~~ scala
186214
abstract class BarEarlyInit(val name: String) extends Bar
187215

188216
object Foo extends BarEarlyInit("Foo")
189-
```
217+
~~~
190218

191219
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
194221
class Fizz private (val name: String) extends Bar {
195222
def this() = this("Fizz")
196223
}
197-
```
224+
~~~
225+
{% endtab %}
226+
{% endtabs %}
198227

199228
## Existential Type
200229

201230
Existential type is a [dropped feature]({{ site.scala3ref }}/dropped-features/existential-types.html), which makes the following code invalid.
202-
203-
```scala
204-
def foo: List[Class[T]] forSome { type T } // Error: Existential types are no longer supported
205-
```
231+
232+
{% tabs scala-2-existential_1 %}
233+
{% tab 'Scala 2 Only' %}
234+
~~~ scala
235+
def foo: List[Class[T]] forSome { type T } // In Scala 3, Error: Existential types are no longer supported
236+
~~~
237+
{% endtab %}
238+
{% endtabs %}
206239

207240
> 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.
208241
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:
210243

211-
```scala
244+
{% tabs shared-existential_1 %}
245+
{% tab 'Scala 2 and 3' %}
246+
~~~ scala
212247
trait Bar {
213248
type T
214249
val value: List[Class[T]]
215250
}
216251

217252
def foo: Bar
218-
```
253+
~~~
254+
{% endtab %}
255+
{% endtabs %}
219256

220257
Note that using a wildcard argument, `_` or `?`, is often simpler but is not always possible.
221258
For instance you could replace `List[T] forSome { type T }` by `List[?]`.

0 commit comments

Comments
 (0)