Skip to content

Commit 8273b8a

Browse files
authored
Add code tabs to Syntactic Changes (#2722)
1 parent 6d303fa commit 8273b8a

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

_overviews/scala3-migration/incompat-syntactic.md

+48-30
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,21 @@ It is composed of:
3838
- `=>>`
3939
- `?=>`
4040

41-
For instance, the following piece of code can be compiled with Scala 2.13 but not wtih Scala 3.
41+
{% tabs scala-2-keywords_1 %}
42+
{% tab 'Scala 2 Only' %}
4243

43-
```scala
44-
object given { // Error: given is now a keyword
45-
val enum = ??? // Error: enum is now a keyword
44+
For instance, the following piece of code can be compiled with Scala 2.13 but not with Scala 3.
45+
~~~ scala
46+
object given { // In Scala 3, Error: given is now a keyword.
47+
val enum = ??? // In Scala 3, Error: enum is now a keyword.
4648

47-
println(enum) // Error: enum is now a keyword
49+
println(enum) // In Scala 3, Error: enum is now a keyword.
4850
}
49-
```
51+
~~~
52+
{% endtab %}
53+
{% endtabs %}
5054

5155
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into:
52-
5356
{% highlight diff %}
5457
-object given {
5558
+object `given` {
@@ -64,18 +67,22 @@ The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the co
6467
## Procedure Syntax
6568

6669
Procedure syntax has been deprecated for a while and it is dropped in Scala 3.
67-
The following pieces of code are now illegal:
6870

69-
```scala
71+
{% tabs scala-2-procedure_1 %}
72+
{% tab 'Scala 2 Only' %}
73+
74+
The following pieces of code are now illegal:
75+
~~~ scala
7076
object Bar {
71-
def print() { // Error: Procedure syntax no longer supported; `: Unit =` should be inserted here
77+
def print() { // In Scala 3, Error: Procedure syntax no longer supported; `: Unit =` should be inserted here.
7278
println("bar")
7379
}
7480
}
75-
```
81+
~~~
82+
{% endtab %}
83+
{% endtabs %}
7684

7785
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into.
78-
7986
{% highlight diff %}
8087
object Bar {
8188
- def print() {
@@ -90,12 +97,15 @@ object Bar {
9097
When followed by its type, the parameter of a lambda is now required to be enclosed in parentheses.
9198
The following piece of code is invalid.
9299

93-
```scala
94-
val f = { x: Int => x * x } // Error: parentheses are required around the parameter of a lambda
95-
```
100+
{% tabs scala-2-lambda_1 %}
101+
{% tab 'Scala 2 Only' %}
102+
~~~ scala
103+
val f = { x: Int => x * x } // In Scala 3, Error: parentheses are required around the parameter of a lambda.
104+
~~~
105+
{% endtab %}
106+
{% endtabs %}
96107

97108
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into:
98-
99109
{% highlight diff %}
100110
-val f = { x: Int => x * x }
101111
+val f = { (x: Int) => x * x }
@@ -106,16 +116,18 @@ The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the co
106116
In Scala 2 it is possible to pass an argument after a new line by enclosing it into braces.
107117
Although valid, this style of coding is not encouraged by the [Scala style guide](https://docs.scala-lang.org/style) and is no longer supported in Scala 3.
108118

109-
This syntax is now invalid:
110-
```scala
119+
{% tabs scala-2-brace_1 %}
120+
{% tab 'Scala 2 Only' %}
121+
~~~ scala
111122
test("my test")
112-
{ // Error: This opening brace will start a new statement in Scala 3.
123+
{ // In Scala 3, Error: This opening brace will start a new statement.
113124
assert(1 == 1)
114125
}
115-
```
126+
~~~
127+
{% endtab %}
128+
{% endtabs %}
116129

117130
The [Scala 3 migration compiler](tooling-migration-mode.html) indents the first line of the block.
118-
119131
{% highlight diff %}
120132
test("my test")
121133
-{
@@ -135,7 +147,6 @@ type Bar = Foo
135147
{% endhighlight %}
136148

137149
A preferable solution is to write:
138-
139150
{% highlight diff %}
140151
-test("my test")
141152
-{
@@ -149,16 +160,20 @@ A preferable solution is to write:
149160
The Scala 3 compiler now requires correct indentation.
150161
The following piece of code, that was compiled in Scala 2.13, does not compile anymore because of the indentation.
151162

152-
```scala
163+
{% tabs scala-2-indentation_1 %}
164+
{% tab 'Scala 2 Only' %}
165+
166+
~~~ scala
153167
def bar: (Int, Int) = {
154168
val foo = 1.0
155-
val bar = foo // [E050] Type Error: value foo does not take parameters
169+
val bar = foo // [E050] In Scala 3, type Error: value foo does not take parameters.
156170
(1, 1)
157-
} // [E007] Type Mismatch Error: Found Unit, Required (Int, Int)
158-
```
171+
} // [E007] In Scala 3, type Mismatch Error: Found Unit, Required (Int, Int).
172+
~~~
173+
{% endtab %}
174+
{% endtabs %}
159175

160176
The indentation must be fixed.
161-
162177
{% highlight diff %}
163178
def bar: (Int, Int) = {
164179
val foo = 1.0
@@ -176,10 +191,13 @@ Beware that these tools may change the entire code style of your project.
176191
The usage of the `_` identifier as a type parameter is permitted in Scala 2.13, even if it has never been mentioned in the Scala 2 specification.
177192
It is used in the API of [fastparse](https://index.scala-lang.org/lihaoyi/fastparse), in combination with a context bound, to declare an implicit parameter.
178193

179-
180-
```scala
194+
{% tabs scala-2-identifier_1 %}
195+
{% tab 'Scala 2 Only' %}
196+
~~~ scala
181197
def foo[_: Foo]: Unit = ???
182-
```
198+
~~~
199+
{% endtab %}
200+
{% endtabs %}
183201

184202
Here, the method `foo` takes a type parameter `_` and an implicit parameter of type `Foo[_]` where `_` refers to the type parameter, not the wildcard symbol.
185203

0 commit comments

Comments
 (0)