Each level of indentation is 2 spaces. Tabs are not used. Thus, instead ofindenting like this:

  1. // wrong!
  2. class Foo {
  3. def fourspaces = {
  4. val x = 4
  5. ..
  6. }
  7. }

You should indent like this:

  1. // right!
  2. class Foo {
  3. def twospaces = {
  4. val x = 2
  5. ..
  6. }
  7. }

The Scala language encourages a startling amount of nested scopes andlogical blocks (function values and such). Do yourself a favor and don’tpenalize yourself syntactically for opening up a new block. Coming fromJava, this style does take a bit of getting used to, but it is wellworth the effort.

Line Wrapping

There are times when a single expression reaches a length where itbecomes unreadable to keep it confined to a single line (usually thatlength is anywhere above 80 characters). In such cases, the _preferred_approach is to simply split the expression up into multiple expressionsby assigning intermediate results to values. However, this is notalways a practical solution.

When it is absolutely necessary to wrap an expression across more thanone line, each successive line should be indented two spaces from thefirst. Also remember that Scala requires each “wrap line” to eitherhave an unclosed parenthetical or to end with an infix method in whichthe right parameter is not given:

  1. val result = 1 + 2 + 3 + 4 + 5 + 6 +
  2. 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 +
  3. 15 + 16 + 17 + 18 + 19 + 20

Without this trailing method, Scala will infer a semi-colon at the endof a line which was intended to wrap, throwing off the compilationsometimes without even so much as a warning.

Methods with Numerous Arguments

When calling a method which takes numerous arguments (in the range offive or more), it is often necessary to wrap the method invocation ontomultiple lines. In such cases, put each argument on a line byitself, indented two spaces from the current indent level:

  1. foo(
  2. someVeryLongFieldName,
  3. andAnotherVeryLongFieldName,
  4. "this is a string",
  5. 3.1415)

This way, all parameters line up, but you don’t need to re-align them ifyou change the name of the method later on.

Great care should be taken to avoid these sorts of invocations well intothe length of the line. More specifically, such an invocation should beavoided when each parameter would have to be indented more than 50spaces to achieve alignment. In such cases, the invocation itself shouldbe moved to the next line and indented two spaces:

  1. // right!
  2. val myLongFieldNameWithNoRealPoint =
  3. foo(
  4. someVeryLongFieldName,
  5. andAnotherVeryLongFieldName,
  6. "this is a string",
  7. 3.1415)
  8. // wrong!
  9. val myLongFieldNameWithNoRealPoint = foo(someVeryLongFieldName,
  10. andAnotherVeryLongFieldName,
  11. "this is a string",
  12. 3.1415)

Better yet, just try to avoid any method which takes more than two orthree parameters!