From 7cbeea10e2f6b7290f287233d34bfc69376c7e4c Mon Sep 17 00:00:00 2001 From: Kisaragi <48310258+KisaragiEffective@users.noreply.github.com> Date: Sat, 19 Nov 2022 19:43:08 +0900 Subject: [PATCH 1/2] Add code tabs to the Scala 3 syntax rewriting --- .../tooling-syntax-rewriting.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/_overviews/scala3-migration/tooling-syntax-rewriting.md b/_overviews/scala3-migration/tooling-syntax-rewriting.md index 67898458e6..c011acbd80 100644 --- a/_overviews/scala3-migration/tooling-syntax-rewriting.md +++ b/_overviews/scala3-migration/tooling-syntax-rewriting.md @@ -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) { @@ -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`). @@ -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 scala-3-location_1 %} +{% tab 'Scala 3 Only' %} ```scala // build.sbt 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) { @@ -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. @@ -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_2 %} +{% tab 'Scala 3 Only' %} ```scala case class State(n: Int, minValue: Int, maxValue: Int): @@ -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 @@ -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_3 %} +{% tab 'Scala 3 Only' %} ```scala case class State(n: Int, minValue: Int, maxValue: Int) { @@ -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) { @@ -191,6 +213,8 @@ case class State(n: Int, minValue: Int, maxValue: Int) { } } ``` +{% endtab %} +{% endtabs %} With this last rewrite, we have come full circle. From 61580cf0253da609308b2d37a34512a9e2fee8fa Mon Sep 17 00:00:00 2001 From: Kisaragi <48310258+KisaragiEffective@users.noreply.github.com> Date: Wed, 23 Nov 2022 12:38:42 +0900 Subject: [PATCH 2/2] fix: fix various issue --- .../scala3-migration/tooling-syntax-rewriting.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_overviews/scala3-migration/tooling-syntax-rewriting.md b/_overviews/scala3-migration/tooling-syntax-rewriting.md index c011acbd80..e6cc2cba32 100644 --- a/_overviews/scala3-migration/tooling-syntax-rewriting.md +++ b/_overviews/scala3-migration/tooling-syntax-rewriting.md @@ -96,10 +96,10 @@ 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 scala-3-location_1 %} -{% tab 'Scala 3 Only' %} +{% tabs sbt-location %} +{% tab 'sbt' %} ```scala -// build.sbt +// build.sbt, for Scala 3 project scalacOptions ++= Seq("-new-syntax", "-rewrite") ``` {% endtab %} @@ -138,7 +138,7 @@ 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_2 %} +{% tabs scala-3-location_3 %} {% tab 'Scala 3 Only' %} ```scala case class State(n: Int, minValue: Int, maxValue: Int): @@ -166,7 +166,7 @@ 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_3 %} +{% tabs scala-3-location_4 %} {% tab 'Scala 3 Only' %} ```scala case class State(n: Int, minValue: Int, maxValue: Int) {