...

/

Nested Blocks

Nested Blocks

In this lesson, we are going to go over how to place curly brackets and parentheses for nested blocks.

Curly Brackets

Opening curly brackets ({) must be on the same line as the declaration they represent.

Press + to interact
def foo = {
...
}

Technically, Scala’s parser does support GNU-style notation with opening brackets on the line following the declaration. However, the parser is not terribly predictable when dealing with this style due to the way in which semi-colon inference is implemented. Many headaches will be saved by simply following the curly bracket convention demonstrated above.

Parentheses

In the rare cases when parenthetical blocks wrap across lines, the opening and closing parentheses should be unspaced and generally kept on the same lines as their content (Lisp-style).

Press + to interact
(this + is a very ++ long *
expression)

Parentheses also serve to disable semicolon inference, and so allow the developer to start lines with operators, which some prefer.

Press + to interact
( someCondition
|| someOtherCondition
|| thirdCondition
)

A trailing parenthesis on the following line is acceptable in this case, for aesthetic reasons.