From c7fae52153bc5662198122fa0f7f426757d74012 Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 8 Mar 2023 11:27:34 +0100 Subject: [PATCH 1/7] Add code tabs to Syntactic Changes --- Gemfile.lock | 3 + .../scala3-migration/incompat-syntactic.md | 175 +++++++++++------- 2 files changed, 110 insertions(+), 68 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9a1627c8b8..cb30f39c76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -219,6 +219,8 @@ GEM jekyll-feed (~> 0.9) jekyll-seo-tag (~> 2.1) minitest (5.17.0) + nokogiri (1.14.2-x86_64-darwin) + racc (~> 1.4) nokogiri (1.14.2-x86_64-linux) racc (~> 1.4) octokit (4.25.1) @@ -263,6 +265,7 @@ GEM zeitwerk (2.6.7) PLATFORMS + x86_64-darwin-22 x86_64-linux DEPENDENCIES diff --git a/_overviews/scala3-migration/incompat-syntactic.md b/_overviews/scala3-migration/incompat-syntactic.md index 35846f886f..d19d3ad7ed 100644 --- a/_overviews/scala3-migration/incompat-syntactic.md +++ b/_overviews/scala3-migration/incompat-syntactic.md @@ -38,135 +38,160 @@ It is composed of: - `=>>` - `?=>` -For instance, the following piece of code can be compiled with Scala 2.13 but not wtih Scala 3. +{% tabs keywords_1 class=tabs-scala-version %} +{% tab 'Scala 2' for=keywords_1 %} -```scala -object given { // Error: given is now a keyword - val enum = ??? // Error: enum is now a keyword +For instance, the following piece of code can be compiled with Scala 2.13 but not with Scala 3. +~~~ scala +object given { //Error: given is now a keyword + val enum = ??? //Error: enum is now a keyword - println(enum) // Error: enum is now a keyword + println(enum) //Error: enum is now a keyword } -``` +~~~ +{% endtab %} +{% tab 'Scala 3' for=keywords_1 %} The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into: +~~~ scala +object `given` { + val `enum` = ??? -{% highlight diff %} --object given { -+object `given` { -- val enum = ??? -+ val `enum` = ??? - -- println(enum) -+ println(`enum`) + println(`enum`) } -{% endhighlight %} +~~~ +{% endtab %} +{% endtabs %} ## Procedure Syntax Procedure syntax has been deprecated for a while and it is dropped in Scala 3. -The following pieces of code are now illegal: -```scala +{% tabs procedure_1 class=tabs-scala-version %} +{% tab 'Scala 2' for=procedure_1 %} + +The following pieces of code are now illegal: +~~~ scala object Bar { - def print() { // Error: Procedure syntax no longer supported; `: Unit =` should be inserted here + def print() { //Error: Procedure syntax no longer supported; `: Unit =` should be inserted here println("bar") } } -``` +~~~ +{% endtab %} +{% tab 'Scala 3' for=procedure_1 %} The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into. - -{% highlight diff %} +~~~ scala object Bar { -- def print() { -+ def print(): Unit = { + def print(): Unit = { println("bar") } } -{% endhighlight %} +~~~ +{% endtab %} +{% endtabs %} ## Parentheses Around Lambda Parameter When followed by its type, the parameter of a lambda is now required to be enclosed in parentheses. The following piece of code is invalid. -```scala -val f = { x: Int => x * x } // Error: parentheses are required around the parameter of a lambda -``` +{% tabs lambda_1 class=tabs-scala-version %} +{% tab 'Scala 2' for=lambda_1 %} +~~~ scala +val f = { x: Int => x * x } //Error: parentheses are required around the parameter of a lambda +~~~ +{% endtab %} +{% tab 'Scala 3' for=lambda_1 %} The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into: - -{% highlight diff %} --val f = { x: Int => x * x } -+val f = { (x: Int) => x * x } -{% endhighlight %} +~~~ scala +val f = { (x: Int) => x * x } +~~~ +{% endtab %} +{% endtabs %} ## Open Brace Indentation For Passing An Argument In Scala 2 it is possible to pass an argument after a new line by enclosing it into braces. 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. -This syntax is now invalid: -```scala +{% tabs brace_1 class=tabs-scala-version %} +{% tab 'Scala 2' for=brace_1 %} +~~~ scala test("my test") -{ // Error: This opening brace will start a new statement in Scala 3. +{ assert(1 == 1) } -``` +~~~ +{% endtab %} +{% tab 'Scala 3' for=brace_1 %} The [Scala 3 migration compiler](tooling-migration-mode.html) indents the first line of the block. - -{% highlight diff %} +~~~ scala test("my test") --{ -+ { + { assert(1 == 1) } -{% endhighlight %} +~~~ +{% endtab %} +{% endtabs %} This migration rule applies to other patterns as well, such as refining a type after a new line. -{% highlight diff %} +{% tabs scala-3-brace_2 %} +{% tab 'Scala 3 Only' %} +~~~ scala type Bar = Foo -- { -+ { + { def bar(): Int } -{% endhighlight %} +~~~ +{% endtab %} +{% endtabs %} A preferable solution is to write: -{% highlight diff %} --test("my test") --{ -+test("my test") { +{% tabs scala-3-brace_3 %} +{% tab 'Scala 3 Only' %} +~~~ scala +test("my test") { assert(1 == 1) } -{% endhighlight %} +~~~ +{% endtab %} +{% endtabs %} + ## Wrong indentation The Scala 3 compiler now requires correct indentation. The following piece of code, that was compiled in Scala 2.13, does not compile anymore because of the indentation. -```scala +{% tabs indentation_1 class=tabs-scala-version %} +{% tab 'Scala 2' for=indentation_1 %} + +~~~ scala def bar: (Int, Int) = { val foo = 1.0 val bar = foo // [E050] Type Error: value foo does not take parameters (1, 1) } // [E007] Type Mismatch Error: Found Unit, Required (Int, Int) -``` +~~~ +{% endtab %} +{% tab 'Scala 3' for=indentation_1 %} The indentation must be fixed. - -{% highlight diff %} +~~~ scala def bar: (Int, Int) = { val foo = 1.0 val bar = foo -- (1, 1) -+ (1, 1) + (1, 1) } -{% endhighlight %} +~~~ +{% endtab %} +{% endtabs %} These errors can be prevented by using a Scala formatting tool such as [scalafmt](https://scalameta.org/scalafmt/) or the [IntelliJ Scala formatter](https://www.jetbrains.com/help/idea/reformat-and-rearrange-code.html). Beware that these tools may change the entire code style of your project. @@ -176,10 +201,13 @@ Beware that these tools may change the entire code style of your project. 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. 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. - -```scala +{% tabs scala-2-identifier_1 %} +{% tab 'Scala 2 Only' %} +~~~ scala def foo[_: Foo]: Unit = ??? -``` +~~~ +{% endtab %} +{% endtabs %} 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. @@ -187,20 +215,27 @@ Martin Odersky described this pattern as a "clever exploit of a scalac compiler The Scala 3 compiler does not permit this pattern anymore: -{% highlight text %} +{% tabs scala-3-identifier_2 %} +{% tab 'Scala 3 Only' %} +~~~ scala -- [E040] Syntax Error: src/main/scala/anonymous-type-param.scala:4:10 4 | def foo[_: Foo]: Unit = () | ^ | an identifier expected, but '_' found -{% endhighlight %} +~~~ +{% endtab %} +{% endtabs %} The solution is to give the parameter a valid identifier name, for instance `T`. This will not break the binary compatibility. -{% highlight diff %} --def foo[_: Foo]: Unit = ??? -+def foo[T: Foo]: Unit = ??? -{% endhighlight %} +{% tabs scala-3-identifier_3 %} +{% tab 'Scala 3 Only' %} +~~~ scala +def foo[T: Foo]: Unit = ??? +~~~ +{% endtab %} +{% endtabs %} ## `+` And `-` As Type Parameters @@ -208,12 +243,16 @@ This will not break the binary compatibility. You cannot write `def foo[+]` or `def foo[-]` anymore. -{% highlight text %} +{% tabs scala-3-identifier_4 %} +{% tab 'Scala 3 Only' %} +~~~ scala -- Error: src/main/scala/type-param-identifier.scala:2:10 2 | def foo[+]: + | ^ | no `+/-` variance annotation allowed here -{% endhighlight %} +~~~ +{% endtab %} +{% endtabs %} The solution is to choose another valid identifier, for instance `T`. From f30c2e3f789e6400686df43e6cc59fab13e9fb7f Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 8 Mar 2023 11:30:12 +0100 Subject: [PATCH 2/7] Delete weirds dependencies --- Gemfile.lock | 3 --- 1 file changed, 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index cb30f39c76..9a1627c8b8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -219,8 +219,6 @@ GEM jekyll-feed (~> 0.9) jekyll-seo-tag (~> 2.1) minitest (5.17.0) - nokogiri (1.14.2-x86_64-darwin) - racc (~> 1.4) nokogiri (1.14.2-x86_64-linux) racc (~> 1.4) octokit (4.25.1) @@ -265,7 +263,6 @@ GEM zeitwerk (2.6.7) PLATFORMS - x86_64-darwin-22 x86_64-linux DEPENDENCIES From 22c494f5558f83c5e150d293ddab9ccad60c00f9 Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 8 Mar 2023 11:49:15 +0100 Subject: [PATCH 3/7] Change to scala3 only when error --- Gemfile.lock | 3 ++ .../scala3-migration/incompat-syntactic.md | 36 ++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9a1627c8b8..cb30f39c76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -219,6 +219,8 @@ GEM jekyll-feed (~> 0.9) jekyll-seo-tag (~> 2.1) minitest (5.17.0) + nokogiri (1.14.2-x86_64-darwin) + racc (~> 1.4) nokogiri (1.14.2-x86_64-linux) racc (~> 1.4) octokit (4.25.1) @@ -263,6 +265,7 @@ GEM zeitwerk (2.6.7) PLATFORMS + x86_64-darwin-22 x86_64-linux DEPENDENCIES diff --git a/_overviews/scala3-migration/incompat-syntactic.md b/_overviews/scala3-migration/incompat-syntactic.md index d19d3ad7ed..57ba42bfcb 100644 --- a/_overviews/scala3-migration/incompat-syntactic.md +++ b/_overviews/scala3-migration/incompat-syntactic.md @@ -38,8 +38,8 @@ It is composed of: - `=>>` - `?=>` -{% tabs keywords_1 class=tabs-scala-version %} -{% tab 'Scala 2' for=keywords_1 %} +{% tabs scala-3-keywords_1 %} +{% tab 'Scala 3 Only' %} For instance, the following piece of code can be compiled with Scala 2.13 but not with Scala 3. ~~~ scala @@ -50,7 +50,10 @@ object given { //Error: given is now a keyword } ~~~ {% endtab %} -{% tab 'Scala 3' for=keywords_1 %} +{% endtabs %} + +{% tabs scala-3-keywords_2 %} +{% tab 'Scala 3 Only' %} The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into: ~~~ scala @@ -67,8 +70,8 @@ object `given` { Procedure syntax has been deprecated for a while and it is dropped in Scala 3. -{% tabs procedure_1 class=tabs-scala-version %} -{% tab 'Scala 2' for=procedure_1 %} +{% tabs scala-3-procedure_1 %} +{% tab 'Scala 3 Only' %} The following pieces of code are now illegal: ~~~ scala @@ -79,7 +82,10 @@ object Bar { } ~~~ {% endtab %} -{% tab 'Scala 3' for=procedure_1 %} +{% endtabs %} + +{% tabs scala-3-procedure_2 %} +{% tab 'Scala 3 Only' %} The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into. ~~~ scala @@ -97,13 +103,16 @@ object Bar { When followed by its type, the parameter of a lambda is now required to be enclosed in parentheses. The following piece of code is invalid. -{% tabs lambda_1 class=tabs-scala-version %} -{% tab 'Scala 2' for=lambda_1 %} +{% tabs scala-3-lambda_1 %} +{% tab 'Scala 3 Only' %} ~~~ scala val f = { x: Int => x * x } //Error: parentheses are required around the parameter of a lambda ~~~ {% endtab %} -{% tab 'Scala 3' for=lambda_1 %} +{% endtabs %} + +{% tabs scala-3-lambda_2 %} +{% tab 'Scala 3 Only' %} The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into: ~~~ scala @@ -169,8 +178,8 @@ test("my test") { The Scala 3 compiler now requires correct indentation. The following piece of code, that was compiled in Scala 2.13, does not compile anymore because of the indentation. -{% tabs indentation_1 class=tabs-scala-version %} -{% tab 'Scala 2' for=indentation_1 %} +{% tabs scala-3-indentation_1 %} +{% tab 'Scala 3 Only' %} ~~~ scala def bar: (Int, Int) = { @@ -180,7 +189,10 @@ def bar: (Int, Int) = { } // [E007] Type Mismatch Error: Found Unit, Required (Int, Int) ~~~ {% endtab %} -{% tab 'Scala 3' for=indentation_1 %} +{% endtabs %} + +{% tabs scala-3-indentation_2 %} +{% tab 'Scala 3 Only' %} The indentation must be fixed. ~~~ scala From 9ea25e9cf835ab6f400fb2895676b12e0476edd2 Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 8 Mar 2023 12:05:33 +0100 Subject: [PATCH 4/7] revert file --- Gemfile.lock | 3 --- 1 file changed, 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index cb30f39c76..9a1627c8b8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -219,8 +219,6 @@ GEM jekyll-feed (~> 0.9) jekyll-seo-tag (~> 2.1) minitest (5.17.0) - nokogiri (1.14.2-x86_64-darwin) - racc (~> 1.4) nokogiri (1.14.2-x86_64-linux) racc (~> 1.4) octokit (4.25.1) @@ -265,7 +263,6 @@ GEM zeitwerk (2.6.7) PLATFORMS - x86_64-darwin-22 x86_64-linux DEPENDENCIES From 91c927c84b6df2a0536b94113e067a33256f11cf Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 9 Mar 2023 10:40:16 +0100 Subject: [PATCH 5/7] Corrections --- Gemfile.lock | 3 + .../scala3-migration/incompat-syntactic.md | 68 +++++++------------ 2 files changed, 29 insertions(+), 42 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9a1627c8b8..cb30f39c76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -219,6 +219,8 @@ GEM jekyll-feed (~> 0.9) jekyll-seo-tag (~> 2.1) minitest (5.17.0) + nokogiri (1.14.2-x86_64-darwin) + racc (~> 1.4) nokogiri (1.14.2-x86_64-linux) racc (~> 1.4) octokit (4.25.1) @@ -263,6 +265,7 @@ GEM zeitwerk (2.6.7) PLATFORMS + x86_64-darwin-22 x86_64-linux DEPENDENCIES diff --git a/_overviews/scala3-migration/incompat-syntactic.md b/_overviews/scala3-migration/incompat-syntactic.md index 57ba42bfcb..f86b3e170b 100644 --- a/_overviews/scala3-migration/incompat-syntactic.md +++ b/_overviews/scala3-migration/incompat-syntactic.md @@ -38,45 +38,43 @@ It is composed of: - `=>>` - `?=>` -{% tabs scala-3-keywords_1 %} -{% tab 'Scala 3 Only' %} +{% tabs scala-2-keywords_1 %} +{% tab 'Scala 2 Only' %} For instance, the following piece of code can be compiled with Scala 2.13 but not with Scala 3. ~~~ scala -object given { //Error: given is now a keyword - val enum = ??? //Error: enum is now a keyword +object given { // Error: given is now a keyword + val enum = ??? // Error: enum is now a keyword - println(enum) //Error: enum is now a keyword + println(enum) // Error: enum is now a keyword } ~~~ {% endtab %} {% endtabs %} -{% tabs scala-3-keywords_2 %} -{% tab 'Scala 3 Only' %} - The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into: -~~~ scala -object `given` { - val `enum` = ??? - - println(`enum`) +{% highlight diff %} +-object given { ++object `given` { +- val enum = ??? ++ val `enum` = ??? + +- println(enum) ++ println(`enum`) } -~~~ -{% endtab %} -{% endtabs %} +{% endhighlight %} ## Procedure Syntax Procedure syntax has been deprecated for a while and it is dropped in Scala 3. -{% tabs scala-3-procedure_1 %} -{% tab 'Scala 3 Only' %} +{% tabs scala-2-procedure_1 %} +{% tab 'Scala 2 Only' %} The following pieces of code are now illegal: ~~~ scala object Bar { - def print() { //Error: Procedure syntax no longer supported; `: Unit =` should be inserted here + def print() { // Error: Procedure syntax no longer supported; `: Unit =` should be inserted here println("bar") } } @@ -103,17 +101,16 @@ object Bar { When followed by its type, the parameter of a lambda is now required to be enclosed in parentheses. The following piece of code is invalid. -{% tabs scala-3-lambda_1 %} -{% tab 'Scala 3 Only' %} +{% tabs scala-2-lambda_1 %} +{% tab 'Scala 2 Only' %} ~~~ scala -val f = { x: Int => x * x } //Error: parentheses are required around the parameter of a lambda +val f = { x: Int => x * x } // Error: parentheses are required around the parameter of a lambda ~~~ {% endtab %} {% endtabs %} {% tabs scala-3-lambda_2 %} {% tab 'Scala 3 Only' %} - The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into: ~~~ scala val f = { (x: Int) => x * x } @@ -157,13 +154,8 @@ type Bar = Foo def bar(): Int } ~~~ -{% endtab %} -{% endtabs %} A preferable solution is to write: - -{% tabs scala-3-brace_3 %} -{% tab 'Scala 3 Only' %} ~~~ scala test("my test") { assert(1 == 1) @@ -178,8 +170,8 @@ test("my test") { The Scala 3 compiler now requires correct indentation. The following piece of code, that was compiled in Scala 2.13, does not compile anymore because of the indentation. -{% tabs scala-3-indentation_1 %} -{% tab 'Scala 3 Only' %} +{% tabs scala-2-indentation_1 %} +{% tab 'Scala 2 Only' %} ~~~ scala def bar: (Int, Int) = { @@ -227,16 +219,12 @@ Martin Odersky described this pattern as a "clever exploit of a scalac compiler The Scala 3 compiler does not permit this pattern anymore: -{% tabs scala-3-identifier_2 %} -{% tab 'Scala 3 Only' %} -~~~ scala +{% highlight text %} -- [E040] Syntax Error: src/main/scala/anonymous-type-param.scala:4:10 4 | def foo[_: Foo]: Unit = () | ^ | an identifier expected, but '_' found -~~~ -{% endtab %} -{% endtabs %} +{% endhighlight %} The solution is to give the parameter a valid identifier name, for instance `T`. This will not break the binary compatibility. @@ -255,16 +243,12 @@ def foo[T: Foo]: Unit = ??? You cannot write `def foo[+]` or `def foo[-]` anymore. -{% tabs scala-3-identifier_4 %} -{% tab 'Scala 3 Only' %} -~~~ scala +{% highlight text %} -- Error: src/main/scala/type-param-identifier.scala:2:10 2 | def foo[+]: + | ^ | no `+/-` variance annotation allowed here -~~~ -{% endtab %} -{% endtabs %} +{% endhighlight %} The solution is to choose another valid identifier, for instance `T`. From 95ab32488f3b95025d9830a095a5c23aa0796f7f Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 9 Mar 2023 10:42:10 +0100 Subject: [PATCH 6/7] Remove dependencies --- Gemfile.lock | 3 --- 1 file changed, 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index cb30f39c76..9a1627c8b8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -219,8 +219,6 @@ GEM jekyll-feed (~> 0.9) jekyll-seo-tag (~> 2.1) minitest (5.17.0) - nokogiri (1.14.2-x86_64-darwin) - racc (~> 1.4) nokogiri (1.14.2-x86_64-linux) racc (~> 1.4) octokit (4.25.1) @@ -265,7 +263,6 @@ GEM zeitwerk (2.6.7) PLATFORMS - x86_64-darwin-22 x86_64-linux DEPENDENCIES From 9424ebe47925bedfb2b94313a1be02780f20d6ad Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 9 Mar 2023 16:58:21 +0100 Subject: [PATCH 7/7] Corrections --- .../scala3-migration/incompat-syntactic.md | 97 ++++++++----------- 1 file changed, 40 insertions(+), 57 deletions(-) diff --git a/_overviews/scala3-migration/incompat-syntactic.md b/_overviews/scala3-migration/incompat-syntactic.md index f86b3e170b..ddf046940d 100644 --- a/_overviews/scala3-migration/incompat-syntactic.md +++ b/_overviews/scala3-migration/incompat-syntactic.md @@ -43,10 +43,10 @@ It is composed of: For instance, the following piece of code can be compiled with Scala 2.13 but not with Scala 3. ~~~ scala -object given { // Error: given is now a keyword - val enum = ??? // Error: enum is now a keyword +object given { // In Scala 3, Error: given is now a keyword. + val enum = ??? // In Scala 3, Error: enum is now a keyword. - println(enum) // Error: enum is now a keyword + println(enum) // In Scala 3, Error: enum is now a keyword. } ~~~ {% endtab %} @@ -74,7 +74,7 @@ Procedure syntax has been deprecated for a while and it is dropped in Scala 3. The following pieces of code are now illegal: ~~~ scala object Bar { - def print() { // Error: Procedure syntax no longer supported; `: Unit =` should be inserted here + def print() { // In Scala 3, Error: Procedure syntax no longer supported; `: Unit =` should be inserted here. println("bar") } } @@ -82,19 +82,15 @@ object Bar { {% endtab %} {% endtabs %} -{% tabs scala-3-procedure_2 %} -{% tab 'Scala 3 Only' %} - The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into. -~~~ scala +{% highlight diff %} object Bar { - def print(): Unit = { +- def print() { ++ def print(): Unit = { println("bar") } } -~~~ -{% endtab %} -{% endtabs %} +{% endhighlight %} ## Parentheses Around Lambda Parameter @@ -104,66 +100,60 @@ The following piece of code is invalid. {% tabs scala-2-lambda_1 %} {% tab 'Scala 2 Only' %} ~~~ scala -val f = { x: Int => x * x } // Error: parentheses are required around the parameter of a lambda +val f = { x: Int => x * x } // In Scala 3, Error: parentheses are required around the parameter of a lambda. ~~~ {% endtab %} {% endtabs %} -{% tabs scala-3-lambda_2 %} -{% tab 'Scala 3 Only' %} The [Scala 3 migration compilation](tooling-migration-mode.html) rewrites the code into: -~~~ scala -val f = { (x: Int) => x * x } -~~~ -{% endtab %} -{% endtabs %} +{% highlight diff %} +-val f = { x: Int => x * x } ++val f = { (x: Int) => x * x } +{% endhighlight %} ## Open Brace Indentation For Passing An Argument In Scala 2 it is possible to pass an argument after a new line by enclosing it into braces. 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. -{% tabs brace_1 class=tabs-scala-version %} -{% tab 'Scala 2' for=brace_1 %} +{% tabs scala-2-brace_1 %} +{% tab 'Scala 2 Only' %} ~~~ scala test("my test") -{ +{ // In Scala 3, Error: This opening brace will start a new statement. assert(1 == 1) } ~~~ {% endtab %} -{% tab 'Scala 3' for=brace_1 %} +{% endtabs %} The [Scala 3 migration compiler](tooling-migration-mode.html) indents the first line of the block. -~~~ scala +{% highlight diff %} test("my test") - { +-{ ++ { assert(1 == 1) } -~~~ -{% endtab %} -{% endtabs %} +{% endhighlight %} This migration rule applies to other patterns as well, such as refining a type after a new line. -{% tabs scala-3-brace_2 %} -{% tab 'Scala 3 Only' %} -~~~ scala +{% highlight diff %} type Bar = Foo - { +- { ++ { def bar(): Int } -~~~ +{% endhighlight %} A preferable solution is to write: -~~~ scala -test("my test") { +{% highlight diff %} +-test("my test") +-{ ++test("my test") { assert(1 == 1) } -~~~ -{% endtab %} -{% endtabs %} - +{% endhighlight %} ## Wrong indentation @@ -176,26 +166,22 @@ The following piece of code, that was compiled in Scala 2.13, does not compile a ~~~ scala def bar: (Int, Int) = { val foo = 1.0 - val bar = foo // [E050] Type Error: value foo does not take parameters + val bar = foo // [E050] In Scala 3, type Error: value foo does not take parameters. (1, 1) -} // [E007] Type Mismatch Error: Found Unit, Required (Int, Int) +} // [E007] In Scala 3, type Mismatch Error: Found Unit, Required (Int, Int). ~~~ {% endtab %} {% endtabs %} -{% tabs scala-3-indentation_2 %} -{% tab 'Scala 3 Only' %} - The indentation must be fixed. -~~~ scala +{% highlight diff %} def bar: (Int, Int) = { val foo = 1.0 val bar = foo - (1, 1) +- (1, 1) ++ (1, 1) } -~~~ -{% endtab %} -{% endtabs %} +{% endhighlight %} These errors can be prevented by using a Scala formatting tool such as [scalafmt](https://scalameta.org/scalafmt/) or the [IntelliJ Scala formatter](https://www.jetbrains.com/help/idea/reformat-and-rearrange-code.html). Beware that these tools may change the entire code style of your project. @@ -229,13 +215,10 @@ The Scala 3 compiler does not permit this pattern anymore: The solution is to give the parameter a valid identifier name, for instance `T`. This will not break the binary compatibility. -{% tabs scala-3-identifier_3 %} -{% tab 'Scala 3 Only' %} -~~~ scala -def foo[T: Foo]: Unit = ??? -~~~ -{% endtab %} -{% endtabs %} +{% highlight diff %} +-def foo[_: Foo]: Unit = ??? ++def foo[T: Foo]: Unit = ??? +{% endhighlight %} ## `+` And `-` As Type Parameters