# Lambda syntax is white space sensitive?

**URL:** <https://discuss.kotlinlang.org/t/lambda-syntax-is-white-space-sensitive/1950>\
**Category:** Language Design\
**Created:** [September 2, 2016, 4:03pm UTC](https://discuss.kotlinlang.org/t/lambda-syntax-is-white-space-sensitive/1950 "2016-09-02T16:03:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthewb](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matthewb](https://discuss.kotlinlang.org/u/matthewb)\
**Post date:** [September 2, 2016, 4:03pm UTC](https://discuss.kotlinlang.org/t/lambda-syntax-is-white-space-sensitive/1950/1 "2016-09-02T16:03:32Z")

</div>

I typically format my code putting { on a new line such as

> while (true)  
> {  
> …  
> }

I found it surprising that this is a syntax error

> args.forEach  
> {  
> …   
> }

While, of course, this syntax works

> args.forEach {  
> …  
> }

Is this intentional in language design? or does it fall out of how the language is parsed?

It reminds me of JavaScript where

> return  
> {  
> a:1,  
> b:2  
> }

does not work because optional semi-colons makes this statement ambiguous.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [September 2, 2016, 9:43pm UTC](https://discuss.kotlinlang.org/t/lambda-syntax-is-white-space-sensitive/1950/2 "2016-09-02T21:43:22Z")

</div>

Yes, this is intentional. Kotlin doesn’t have semicolons, and a language without semicolons has to have semantic line breaks.

---

<div class="post-metadata">

**Author:** ![matthewb](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matthewb](https://discuss.kotlinlang.org/u/matthewb)\
**Post date:** [September 3, 2016, 4:10pm UTC](https://discuss.kotlinlang.org/t/lambda-syntax-is-white-space-sensitive/1950/3 "2016-09-03T16:10:54Z")

</div>

Although is somewhat different in that, unlike the JS example, there’s only one possible/parseable interpretation of the code. It’s kinda unfortunate that formatting choices cause syntax errors. I’ll probably fall back to using

> args.forEach(  
> {  
> …  
> })

to avoid mixing styles.

Regards

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [September 3, 2016, 4:12pm UTC](https://discuss.kotlinlang.org/t/lambda-syntax-is-white-space-sensitive/1950/4 "2016-09-03T16:12:51Z")

</div>

That’s not true. Your original code (with a line break after `forEach`) accesses the `forEach` property of the `args` object and then returns a no-argument lambda from the enclosing block. This code normally does not compile because no such property is defined, but it can be parsed perfectly well.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [November 9, 2017, 8:51pm UTC](https://discuss.kotlinlang.org/t/lambda-syntax-is-white-space-sensitive/1950/6 "2017-11-09T20:51:20Z")

</div>

Both versions work. The first version works, too. But it has a different meaning than the second version.

The compiler cannot deduce the correct version, because both versions are correct syntactically.
