@@ -9,22 +9,41 @@ next-page: method-invocation
9
9
---
10
10
11
11
All control structures should be written with a space following the
12
- defining keyword:
13
-
14
- // right!
15
- if (foo) bar else baz
16
- for (i <- 0 to 10) { ... }
17
- while (true) { println("Hello, World!") }
18
-
19
- // wrong!
20
- if(foo) bar else baz
21
- for(i <- 0 to 10) { ... }
22
- while(true) { println("Hello, World!") }
23
-
12
+ defining keyword. In Scala 3 parentheses around the condition should be omitted:
13
+
14
+ {% tabs control_structures_1 class=tabs-scala-version%}
15
+ {% tab 'Scala 2' for=control_structures_1 %}
16
+ ``` scala
17
+ // right!
18
+ if (foo) bar else baz
19
+ for (i <- 0 to 10 ) { ... }
20
+ while (true ) { println(" Hello, World!" ) }
21
+
22
+ // wrong!
23
+ if (foo) bar else baz
24
+ for (i <- 0 to 10 ) { ... }
25
+ while (true ) { println(" Hello, World!" ) }
26
+ ```
27
+ {% endtab %}
28
+ {% tab 'Scala 3' for=control_structures_1 %}
29
+ ``` scala
30
+ // right!
31
+ if foo then bar else baz
32
+ for i <- 0 to 10 do ...
33
+ while true do println(" Hello, World!" )
34
+
35
+ // wrong!
36
+ if (foo) bar else baz
37
+ for (i <- 0 to 10 ) do ...
38
+ while (true ) do println(" Hello, World!" )
39
+ ```
40
+ {% endtab %}
41
+ {% endtabs %}
24
42
25
43
## Curly-Braces
26
44
27
- Curly-braces should be omitted in cases where the control structure
45
+ In Scala 3 using curly-braces is discouraged and the quiet syntax with significant indentation is favoured.
46
+ In Scala 2, curly-braces should be omitted in cases where the control structure
28
47
represents a pure-functional operation and all branches of the control
29
48
structure (relevant to ` if ` /` else ` ) are single-line expressions.
30
49
Remember the following guidelines:
@@ -41,63 +60,84 @@ Remember the following guidelines:
41
60
42
61
<!-- necessary to separate the following example from the above bullet list -->
43
62
44
- val news = if (foo)
45
- goodNews()
46
- else
47
- badNews()
48
-
49
- if (foo) {
50
- println("foo was true")
51
- }
52
-
53
- news match {
54
- case "good" => println("Good news!")
55
- case "bad" => println("Bad news!")
56
- }
63
+ {% tabs control_structures_2 class=tabs-scala-version%}
64
+ {% tab 'Scala 2' for=control_structures_2 %}
65
+ ``` scala
66
+ val news = if (foo)
67
+ goodNews()
68
+ else
69
+ badNews()
70
+
71
+ if (foo) {
72
+ println(" foo was true" )
73
+ }
74
+
75
+ news match {
76
+ case " good" => println(" Good news!" )
77
+ case " bad" => println(" Bad news!" )
78
+ }
79
+ ```
80
+ {% endtab %}
81
+ {% tab 'Scala 3' for=control_structures_2 %}
82
+ ``` scala
83
+ val news = if foo then
84
+ goodNews()
85
+ else
86
+ badNews()
87
+
88
+ if foo then
89
+ println(" foo was true" )
90
+
91
+ news match
92
+ case " good" => println(" Good news!" )
93
+ case " bad" => println(" Bad news!" )
94
+ ```
95
+ {% endtab %}
96
+ {% endtabs %}
57
97
58
98
## Comprehensions
59
99
60
100
Scala has the ability to represent ` for ` -comprehensions with more than
61
101
one generator (usually, more than one ` <- ` symbol). In such cases, there
62
102
are two alternative syntaxes which may be used:
63
103
64
- // wrong!
65
- for (x <- board.rows; y <- board.files)
66
- yield (x, y)
67
-
68
- // right!
69
- for {
70
- x <- board.rows
71
- y <- board.files
72
- } yield (x, y)
104
+ {% tabs control_structures_3 class=tabs-scala-version%}
105
+ {% tab 'Scala 2' for=control_structures_3 %}
106
+ ``` scala
107
+ // wrong!
108
+ for (x <- board.rows; y <- board.files)
109
+ yield (x, y)
110
+
111
+ // right!
112
+ for {
113
+ x <- board.rows
114
+ y <- board.files
115
+ } yield (x, y)
116
+ ```
117
+ {% endtab %}
118
+ {% tab 'Scala 3' for=control_structures_3 %}
119
+ ``` scala
120
+ // wrong!
121
+ for x <- board.rows; y <- board.files
122
+ yield (x, y)
123
+
124
+ // right!
125
+ for
126
+ x <- board.rows
127
+ y <- board.files
128
+ yield (x, y)
129
+ ```
130
+ {% endtab %}
131
+ {% endtabs %}
73
132
74
133
While the latter style is more verbose, it is generally considered
75
134
easier to read and more "scalable" (meaning that it does not become
76
135
obfuscated as the complexity of the comprehension increases). You should
77
136
prefer this form for all ` for ` -comprehensions of more than one
78
137
generator. Comprehensions with only a single generator (e.g.
79
- ` for ( i <- 0 to 10) yield i ` ) should use the first form (parentheses
138
+ ` for i <- 0 to 10 yield i ` ) should use the first form (parentheses
80
139
rather than curly braces).
81
140
82
- The exceptions to this rule are ` for ` -comprehensions which lack a
83
- ` yield ` clause. In such cases, the construct is actually a loop rather
84
- than a functional comprehension and it is usually more readable to
85
- string the generators together between parentheses rather than using the
86
- syntactically-confusing ` } { ` construct:
87
-
88
- // wrong!
89
- for {
90
- x <- board.rows
91
- y <- board.files
92
- } {
93
- printf("(%d, %d)", x, y)
94
- }
95
-
96
- // right!
97
- for (x <- board.rows; y <- board.files) {
98
- printf("(%d, %d)", x, y)
99
- }
100
-
101
141
Finally, ` for ` comprehensions are preferred to chained calls to ` map ` ,
102
142
` flatMap ` , and ` filter ` , as this can get difficult to read (this is one
103
143
of the purposes of the enhanced ` for ` comprehension).
@@ -108,11 +148,22 @@ There are certain situations where it is useful to create a short
108
148
` if ` /` else ` expression for nested use within a larger expression. In
109
149
Java, this sort of case would traditionally be handled by the ternary
110
150
operator (` ? ` /` : ` ), a syntactic device which Scala lacks. In these
111
- situations (and really any time you have a extremely brief ` if ` /` else `
151
+ situations (and really any time you have an extremely brief ` if ` /` else `
112
152
expression) it is permissible to place the "then" and "else" branches on
113
153
the same line as the ` if ` and ` else ` keywords:
114
154
115
- val res = if (foo) bar else baz
155
+ {% tabs control_structures_4 class=tabs-scala-version%}
156
+ {% tab 'Scala 2' for=control_structures_4 %}
157
+ ``` scala
158
+ val res = if (foo) bar else baz
159
+ ```
160
+ {% endtab %}
161
+ {% tab 'Scala 3' for=control_structures_4 %}
162
+ ``` scala
163
+ val res = if foo then bar else baz
164
+ ```
165
+ {% endtab %}
166
+ {% endtabs %}
116
167
117
168
The key here is that readability is not hindered by moving both branches
118
169
inline with the ` if ` /` else ` . Note that this style should never be used
0 commit comments