With the Dotty compiler you can convert Scala 2 code to the new Scala 3 syntax, and with the Dotty 0.20.0-RC1 release on November 4, 2019, I thought I’d see how some of the conversions work. Almost all of the changes shown below have to do with the elimination of curly braces and the use of “significant indentation” syntax, but in one example I also show the then
and do
keywords.
Significant indentation syntax with traits and classes
This first example gives you an idea of how the significant indentation syntax conversion works with traits and classes. First, here’s my original Scala 2 code:
Next, here’s what that code looks like after I compiled it with the Dotty dotc compiler:
(If you’re interested in the background, this code comes from my Scala Factory Pattern example.)
Note that in all of these examples I compile the Scala 2 code to Scala 3 (Dotty) with this command:
dotc -indent -rewrite MyCode.scala
From the dotc
help text, those command-line options mean:
-indent
: Allow significant indentation-rewrite
: When used in conjunction with-language:Scala2
, rewrites sources to migrate to new syntax
There’s also this command-line option, which cannot be used with -indent
:
-new-syntax
: Require `then` and `do` in control expressions
I show an example of the use of this option below.
match expressions
The next example shows how a couple of methods that have match
expressions are converted by Dotty 0.20. First, my Scala 2 code:
Here’s how Dotty transforms that code to Scala 3:
The conversion mostly gets rid of the opening and closing braces. I assume that in the future, the new
keyword will also be removed in these examples.
for loops and if/else (and `do` and `then`)
These next examples show how the curly braces are removed in methods, for loops, and if/else expressions. First, the original Scala 2 code, which comes from my String Utilities project:
Next, here’s the Scala 3 code that’s generated by Dotty:
This example brings up an interesting situation, because instead of using the -indent
option when compiling the code you can use a -new-syntax
option, which results in this code being generated for the genRandomVariableLengthStringWithBlankSpaces
function:
As shown, that code includes the use of the do
keyword with for
, and then
with if
.
try/catch syntax
Next, here’s an example of how Scala’s try/catch syntax is transformed. First, the original Scala 2 code:
Next, here’s the Scala 3 code generated by Dotty:
Personally, in all of these examples I appreciate how much “noise” is removed from my code just by eliminating the curly braces.
A couple of braces not removed
This last example shows a case where a couple of braces aren’t removed automatically. First, the original Scala 2 code:
Next, here’s the Scala 3 code generated by Dotty, which removes most of the braces, except for those associated with the run
method:
In this case I removed those manually just to be sure the code would compile as expected:
According to the schedule we’re still a year away from Scala 3 being released, so I’m sure all of the code generation will get better with time.
More Dotty/Scala 3 changes
These examples mostly show the elimination of curly braces, but of course those aren’t the only changes with Dotty. I previously shared these other Scala 3 examples:
- This example of Scala 3 enums
- Martin Odersky’s example of Union Types
- My example of Intersection Types
- How to create a Scala 3 project with SBT
- Dotty extension functions
For more information on Dotty, see these resources:
All the best,
Al