I changed everything to
with in our own codebase and started to do the same changes in the community-build. But my experience gave me pause. There were some examples of beautiful code (notably in the intent project) that got much worse with the new scheme. This code used -Yindent-colons, which is not yet standard, but could come after 3.0. An example is:
This is legible and clear. No apply operator we invent could even come close. So, I come back to my earlier statement that in theory
with and an apply operator are better but in practice
: is better.
Here’s are the layout axioms we should follow in Scala:
- If some code must be followed by an expression or definition, that expression or definition can be on the next line; no
; is inserted.
- If something can follow on the next line, then several such things can also follow, as long as everything is indented.
- Otherwise, if some code may be followed by an expression or definition, we can give it
on the next line, as long as the previous line ends with a
Note: The “must” in “must be followed” should really mean “must obviously”. It must be immediately clear from the text that something must follow. For instance I would rule out things like
a b c d
since these would rely on counting that
d is an operator. On the other hand, in
it is obvious that
b continues the previous line since
+ is an operator, and we do not support postfix as a standard anymore.
with the third axiom is not needed for classes and friends. But it introduces syntax which looks unnatural for people coming from Scala 2 and it does not solve the problem what to do about function arguments in a satisfactory way.
Nothing at all is also not a better alternative: When reading code, I should know whether a line stands by itself or is continued later.
: achieves that in a very natural and low-key way. If it is missing, there’s a problem with code like that:
def f = 1
f part of
C or not? I should not have to count or guess here.
So, taken together, I’d say it’s back to the original design. We had the best solution all along. It was certainly not for lack of trying that the alternatives got discarded. They were considered extremely seriously but in the end
: is clearly superior.
The one aspect where we have a clash with
: is the
given syntax which still uses
with. That’s a bit of an inconsistency, but we can live with it, IMO. We went though this many times and found no better way to do it.
So, time to stop turning in circles. After by now lots of experience with
: and extensive trials of the alternatives, I conclude that it works overall really well.