You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: overviews/core/_posts/2014-04-08-language-pitfalls.md
+18-5Lines changed: 18 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -209,13 +209,18 @@ Links to related blog posts, papers, discussions.
209
209
210
210
## Parenthesis `( )` vs. `{ }`
211
211
212
-
Description
212
+
Scala's "infix notation" allows arity-1 calls to use braces instead of parenthesis which can aid readability of methods which take a function as an argument. Correctly building the function argument when using parenthesis can sometimes cause trouble.
213
213
214
-
**Reason:**Why does this problem exist or is this limitation in place?
214
+
**Reason:**Closure syntax has some fiddly bits that are relaxed when using braces.
215
215
216
-
**Symptoms:**What kind of behavior or errors does this lead to?
216
+
**Symptoms:**You will often see syntax errors in code that would work if using braces.
217
217
218
-
**Suggested solution or workaround:** General description how to fix it
218
+
scala> List(1,2,3) foreach(x: Int => println(x))
219
+
<console>:1: error: ')' expected but '(' found.
220
+
List(1,2,3) foreach(x: Int => println(x))
221
+
^
222
+
223
+
**Suggested solution or workaround:** When creating anonymous functions to pass to a call prefer braces. Use parenthesis for named or eta-expanded functions.
219
224
220
225
**Example:**
221
226
@@ -225,9 +230,17 @@ Description
225
230
226
231
seq foreach{x: Int => println(x)}
227
232
233
+
Also
234
+
235
+
seq foreach((x: Int) => println(x))
236
+
seq foreach(println)
237
+
228
238
**Further reading:**
229
239
230
-
Links to related blog posts, papers, discussions.
240
+
[The Scala Language Specification: Version 2.9](http://www.scala-lang.org/docu/files/ScalaReference.pdf)
241
+
* S6.12.3 Infix Operations
242
+
* S6.23 Anonymous Functions
243
+
* Chapter B: Change Log - Changes in Version 2.1.7 (19-Jul-2006) - Closure Syntax
0 commit comments