Background -rewrite
The awesome Scala 3 compiler can help with rewriting from old syntax to new syntax if ... then .. else
, for ... do
, while do
. It can also “de-brace” my code. This is done with these handy sbt commands as a one time job when migrating from Scala 2:
sbt> set Compile/scalacOptions := Seq("-rewrite","-new-syntax")
sbt> clean;compile
sbt> set Compile/scalacOptions := Seq("-rewrite","-indent")
sbt> clean;compile
There are corresponding settings "-no-indent"
and "-old-syntax"
that reverse the above rewritings (if used in that order).
Proposal -rewrite -end-markers
I’m opening this thread to discuss the possibility that the Scala 3 compiler also could automatically insert end markers with something like:
sbt> set Compile/scalacOptions := Seq("-rewrite","-end-markers")
And it could be possible to remove end markers with a corresponding "-no-end-markers"
in the spirit of the already existing rewritings.
I’d also like to discuss which out-of-the-box rules for auto-inserting end markers that the compiler should use. One possibility is that end markers are introduced if:
- a method/class/object has more than
N
lines, or - a method/class/object has more than
B
blank lines in-between blocks of code, or - a method/class/object has indentation depth more than
D
where e.g. N >= 10 || B >= 3 || D >=3
then end <name>
is inserted marking the end of the named method/class/object.
I think that could be helpful, as I have during my migration gotten used to rewriting with sbt and the easy rewrite compiler settings.
Alternative solutions
An alternative could be to leave this for formatters, linting or IDEs. But even if such tools would include this cool functionality, preferably with a high degree of customization options, I still think it would be useful to also have “batteries included” in the compiler with an out-of-the-box rewriting with sane defaults for easy migration; if I’m not happy with some inserted/missing end markers I can always fix those instances manually, while getting the bulk done “for free”. Then when I develop new Scala 3 code I can turn on formatters, linting or IDE functions for automatic end markers if I like.
Conclusion
I think end-markers are great for readability, but sometimes its easy to get a bit lazy when migrating and the total happiness in the world could perhaps increase by some easy automation using rewrite
.
What do you think?