[swift-evolution] Lambda function syntax

Alexander Regueiro alexreg at gmail.com
Tue Dec 22 17:45:02 CST 2015


Sorry, but I don’t think you could be more wrong on every point. I’ve clearly detailed why my solution is superior (both factually and in my opinion), and I can only see subjective “dislike this”.

Using “func” or “lambda” outside is uglier than “in” inside the braes? Really? That just boggles my mind. Sure, maybe Ruby does, but the vast majority of languages shun this approach. C# for example has a lovely, crystal clear syntax for lambda expressions. So does Haskell, like I’ve mentioned.

I’ve also explained the big problem with trailing closures already. And closures are really *not* control flow statements, so treating like them is just a fallacy and source of confusion. There are other arguments against it too, like it doesn’t generalise well (at all) to multiple closure arguments.

Ultimately, the key points when judging how worthwhile a feature are 1) how big a feature is it? 2) how much does it add?. I’d say that for trailing closures, the answer to 1) is “moderate”, and 2) is “next to nothing”.

> On 22 Dec 2015, at 06:57, Thorsten Seitz <tseitz42 at icloud.com> wrote:
> 
> 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