Skip to content

Add code tabs to the Scala 3 syntax rewriting #2643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion _overviews/scala3-migration/tooling-syntax-rewriting.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Let's have a look at how this works in a small example.

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

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

Expand All @@ -80,6 +82,8 @@ case class State(n: Int, minValue: Int, maxValue: Int) {
}
}
```
{% endtab %}
{% endtabs %}

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`).

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

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

{% tabs sbt-location %}
{% tab 'sbt' %}
```scala
// build.sbt
// build.sbt, for Scala 3 project
scalacOptions ++= Seq("-new-syntax", "-rewrite")
```
{% endtab %}
{% endtabs %}

After compiling the code, the result looks as follows:

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

Expand All @@ -117,6 +127,8 @@ case class State(n: Int, minValue: Int, maxValue: Int) {
}
}
```
{% endtab %}
{% endtabs %}

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.

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

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

Expand All @@ -142,6 +156,8 @@ case class State(n: Int, minValue: Int, maxValue: Int):
j <- 0 to n
do println(i + j)
```
{% endtab %}
{% endtabs %}

## Moving back to the Classic syntax

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

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

Expand All @@ -169,9 +187,13 @@ case class State(n: Int, minValue: Int, maxValue: Int) {
}
}
```
{% endtab %}
{% endtabs %}

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

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

Expand All @@ -191,6 +213,8 @@ case class State(n: Int, minValue: Int, maxValue: Int) {
}
}
```
{% endtab %}
{% endtabs %}

With this last rewrite, we have come full circle.

Expand Down