[swift-evolution] Lambda function syntax

Thorsten Seitz tseitz42 at icloud.com
Tue Dec 22 00:57:38 CST 2015


Well, I'm actually happy with the current closure syntax as it allows very succinct simple cases and trailing closures as Chris has already pointed out.

> Am 21.12.2015 um 23:44 schrieb Alexander Regueiro via swift-evolution <swift-evolution at swift.org>:
> 
> Okay, I assume you are aware this essentially the same syntax as used in languages like C# and Python, yes? I’m not sure there are any problems in those languages with it.
> 
>> If you dig through (very early) history you’ll see that we had this.  There are a couple of problems with it:
>> 
>> 1) It punishes simple cases like “X.sort { $1 < $0 }”, along with lots of simple map and filter closures.
> 
> Not really. The above example would just be `X.sort func { $1 < $0 }” or "X.sort \ { $1 < $0 }` in my proposed syntax. Also, it would be nice to have all operators implicitly

Having "func" or the backslash crammed in there is really ugly and unreadable IMHO.

And in Haskell you don't have braces for the body to begin with and you would have to enclose the closure in parenthesis if it is part of an expression like your examples so in effect it would look quite similar, i.e. having the parameters within the parenthesis (sure, the semantics are different, but I made argument just to demonstrate that what looks good in one syntactic environment might not look good in another).

>> 2) It reads really weird in trailing closure cases.
> 
> Honestly, I strongly dislike trailing closures. I don’t think they add much, and moreover they use a confusing syntax that make the whole function call look superficially like a function declaration (or indeed the whole thing being a closure).

Trailing closures are a great feature IMHO because they make the code much more readable by allowing constructs to look similar to control flow statements.
This allows creating very readable DSLs.


>> Lets step back: What problems are you trying to solve with the current closure syntax?
> 
> Readability, mainly. I think this is a big improvement. 

Well, I think it's the opposite for the simple cases and for trailing closures. 


> Then there’s similarity with other languages, which is minor, but nice. I don’t know any language that uses a syntax like the current one of Swift.

Smalltalk and Ruby immediately come to mind and I'm sure there are others.

Scala has a trailing closure syntax which is similar to Swift's syntax as well.

-Thorsten 


More information about the swift-evolution mailing list