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-syntactic.md
+48-30
Original file line number
Diff line number
Diff line change
@@ -38,18 +38,21 @@ It is composed of:
38
38
-`=>>`
39
39
-`?=>`
40
40
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' %}
42
43
43
-
```scala
44
-
objectgiven { // Error: given is now a keyword
45
-
valenum=???// 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
+
objectgiven { // In Scala 3, Error: given is now a keyword.
47
+
valenum=???// In Scala 3, Error: enum is now a keyword.
46
48
47
-
println(enum) // Error: enum is now a keyword
49
+
println(enum) //In Scala 3, Error: enum is now a keyword.
48
50
}
49
-
```
51
+
~~~
52
+
{% endtab %}
53
+
{% endtabs %}
50
54
51
55
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into:
52
-
53
56
{% highlight diff %}
54
57
-object given {
55
58
+object `given` {
@@ -64,18 +67,22 @@ The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the co
64
67
## Procedure Syntax
65
68
66
69
Procedure syntax has been deprecated for a while and it is dropped in Scala 3.
67
-
The following pieces of code are now illegal:
68
70
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
70
76
objectBar {
71
-
defprint() { // Error: Procedure syntax no longer supported; `: Unit =` should be inserted here
77
+
defprint() { //In Scala 3, Error: Procedure syntax no longer supported; `: Unit =` should be inserted here.
72
78
println("bar")
73
79
}
74
80
}
75
-
```
81
+
~~~
82
+
{% endtab %}
83
+
{% endtabs %}
76
84
77
85
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into.
78
-
79
86
{% highlight diff %}
80
87
object Bar {
81
88
- def print() {
@@ -90,12 +97,15 @@ object Bar {
90
97
When followed by its type, the parameter of a lambda is now required to be enclosed in parentheses.
91
98
The following piece of code is invalid.
92
99
93
-
```scala
94
-
valf= { 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
+
valf= { x: Int=> x * x } // In Scala 3, Error: parentheses are required around the parameter of a lambda.
104
+
~~~
105
+
{% endtab %}
106
+
{% endtabs %}
96
107
97
108
The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into:
98
-
99
109
{% highlight diff %}
100
110
-val f = { x: Int => x * x }
101
111
+val f = { (x: Int) => x * x }
@@ -106,16 +116,18 @@ The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the co
106
116
In Scala 2 it is possible to pass an argument after a new line by enclosing it into braces.
107
117
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.
108
118
109
-
This syntax is now invalid:
110
-
```scala
119
+
{% tabs scala-2-brace_1 %}
120
+
{% tab 'Scala 2 Only' %}
121
+
~~~scala
111
122
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.
113
124
assert(1==1)
114
125
}
115
-
```
126
+
~~~
127
+
{% endtab %}
128
+
{% endtabs %}
116
129
117
130
The [Scala 3 migration compiler](tooling-migration-mode.html) indents the first line of the block.
118
-
119
131
{% highlight diff %}
120
132
test("my test")
121
133
-{
@@ -135,7 +147,6 @@ type Bar = Foo
135
147
{% endhighlight %}
136
148
137
149
A preferable solution is to write:
138
-
139
150
{% highlight diff %}
140
151
-test("my test")
141
152
-{
@@ -149,16 +160,20 @@ A preferable solution is to write:
149
160
The Scala 3 compiler now requires correct indentation.
150
161
The following piece of code, that was compiled in Scala 2.13, does not compile anymore because of the indentation.
151
162
152
-
```scala
163
+
{% tabs scala-2-indentation_1 %}
164
+
{% tab 'Scala 2 Only' %}
165
+
166
+
~~~scala
153
167
defbar: (Int, Int) = {
154
168
valfoo=1.0
155
-
valbar= foo // [E050] Type Error: value foo does not take parameters
169
+
valbar= foo // [E050] In Scala 3, type Error: value foo does not take parameters.
156
170
(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 %}
159
175
160
176
The indentation must be fixed.
161
-
162
177
{% highlight diff %}
163
178
def bar: (Int, Int) = {
164
179
val foo = 1.0
@@ -176,10 +191,13 @@ Beware that these tools may change the entire code style of your project.
176
191
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.
177
192
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.
178
193
179
-
180
-
```scala
194
+
{% tabs scala-2-identifier_1 %}
195
+
{% tab 'Scala 2 Only' %}
196
+
~~~scala
181
197
deffoo[_: Foo]:Unit=???
182
-
```
198
+
~~~
199
+
{% endtab %}
200
+
{% endtabs %}
183
201
184
202
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.
0 commit comments