Scala 3 indentation question

According to what I’ve heard, scala 3 will have semantic indentation. Does this mean I have to give up the feature that IntelliJ can reindent my code for me? I use that quite heavily now, write code whose indentation I know is wrong, and then when I type the final close brace, it re-indents properly. How can that work if there are no braces in the code?

One of Odersky’s arguments (apparently he is a lover of semantic indentation) is that you have far fewer lines which just contain a close brace. I’ve always found lines with just a close brace silly, but unfortunately editors enforce it. I’d much rather put all the close braces just at the end of the last line of code, but editors don’t support it as far as I can tell.

  def cmpTypeDesignators(a:SimpleTypeD, b:SimpleTypeD):Boolean = {
    if( a == b )
      true
    else if (a.getClass eq b.getClass) {
      a.cmp(b) }
    else {
      // just compare the classes alphabetically
      s"$a.getClass" < s"$b.getClass" }}

Personally I’m happy that the required parens on the if( ...) are going away. I still find this confusing that the stand-alone if requires parens, but guards in pattern matches don’t require them.

I don’t use IntelliJ, and I am not up to date on Scala 3, but I recall reading a few months ago that the braces will be optional (even within the same file), so you should still be able to use them if you wish. I think that’s great.

1 Like

Yes, braces are optional, see here: https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html
“Old style” braces will still work - I guess for “eternity”. You can even enforce old style braces and prohibit significant white space with the compiler flag -noindent. I guess many companies will start their migration from Scala 2 to Scala 3 with -noindent to align coding standard initially. But my guess is that over time the general community will realize the beautiful and readable brace-less code that you can write in Scala 3 - but that is just my guess.

At first I was worried about the indentation syntax as I have seen in beginner Python programming that beginners had difficulties to understand nested scopes beyond one level of nesting. But with optional braces you can still illustrate the end of a scope explicititly and we also have end markers which improve readability greatly IMHO in more complicated/lenghty nesting situations.

So: No worries you can use your old brace style as long as you like and then gradually try out brace-less code.

2 Likes

BTW: Another good thing with the new compiler behavior, even if you stick with braces, is that I expect more help from the compiler in hunting down, often difficult, bugs resulting from forgotten braces due to nice new warnings on bad indentation etc.