Skip to content

Commit 1a82956

Browse files
Add code tabs to the Scala 3 syntax rewriting (#2643)
* Add code tabs to the Scala 3 syntax rewriting * fix: fix various issue
1 parent 31a1150 commit 1a82956

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

_overviews/scala3-migration/tooling-syntax-rewriting.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Let's have a look at how this works in a small example.
6262

6363
Given the following source code written in a Scala 2 style.
6464

65+
{% tabs scala-2-location %}
66+
{% tab 'Scala 2 Only' %}
6567
```scala
6668
case class State(n: Int, minValue: Int, maxValue: Int) {
6769

@@ -80,6 +82,8 @@ case class State(n: Int, minValue: Int, maxValue: Int) {
8082
}
8183
}
8284
```
85+
{% endtab %}
86+
{% endtabs %}
8387

8488
We will be able to move it to new syntax automatically in two steps: first by using the new control structure rewrite (`-new-syntax -rewrite`) and then the significant indentation rewrite (`-indent -rewrite`).
8589

@@ -92,13 +96,19 @@ We will be able to move it to new syntax automatically in two steps: first by us
9296

9397
We can use the `-new-syntax -rewrite` options by adding them to the list of scalac options in our build tool.
9498

99+
{% tabs sbt-location %}
100+
{% tab 'sbt' %}
95101
```scala
96-
// build.sbt
102+
// build.sbt, for Scala 3 project
97103
scalacOptions ++= Seq("-new-syntax", "-rewrite")
98104
```
105+
{% endtab %}
106+
{% endtabs %}
99107

100108
After compiling the code, the result looks as follows:
101109

110+
{% tabs scala-3-location_2 %}
111+
{% tab 'Scala 3 Only' %}
102112
```scala
103113
case class State(n: Int, minValue: Int, maxValue: Int) {
104114

@@ -117,6 +127,8 @@ case class State(n: Int, minValue: Int, maxValue: Int) {
117127
}
118128
}
119129
```
130+
{% endtab %}
131+
{% endtabs %}
120132

121133
Notice that the parentheses around the `n == maxValue` disappeared, as well as the braces around the `i <- minValue to maxValue` and `j <- 0 to n` generators.
122134

@@ -126,6 +138,8 @@ After this first rewrite, we can use the significant indentation syntax to remov
126138
To do that we use the `-indent` option in combination with the `-rewrite` option.
127139
It leads us to the following version:
128140

141+
{% tabs scala-3-location_3 %}
142+
{% tab 'Scala 3 Only' %}
129143
```scala
130144
case class State(n: Int, minValue: Int, maxValue: Int):
131145

@@ -142,6 +156,8 @@ case class State(n: Int, minValue: Int, maxValue: Int):
142156
j <- 0 to n
143157
do println(i + j)
144158
```
159+
{% endtab %}
160+
{% endtabs %}
145161

146162
## Moving back to the Classic syntax
147163

@@ -150,6 +166,8 @@ Starting from the latest state of our code sample, we can move backwards to its
150166
Let's rewrite the code using braces while retaining the new control structures.
151167
After compiling with the `-no-indent -rewrite` options, we obtain the following result:
152168

169+
{% tabs scala-3-location_4 %}
170+
{% tab 'Scala 3 Only' %}
153171
```scala
154172
case class State(n: Int, minValue: Int, maxValue: Int) {
155173

@@ -169,9 +187,13 @@ case class State(n: Int, minValue: Int, maxValue: Int) {
169187
}
170188
}
171189
```
190+
{% endtab %}
191+
{% endtabs %}
172192

173193
Applying one more rewrite, with `-old-syntax -rewrite`, takes us back to the original Scala 2-style code.
174194

195+
{% tabs shared-location %}
196+
{% tab 'Scala 2 and 3' %}
175197
```scala
176198
case class State(n: Int, minValue: Int, maxValue: Int) {
177199

@@ -191,6 +213,8 @@ case class State(n: Int, minValue: Int, maxValue: Int) {
191213
}
192214
}
193215
```
216+
{% endtab %}
217+
{% endtabs %}
194218

195219
With this last rewrite, we have come full circle.
196220

0 commit comments

Comments
 (0)