It's my fault, sorry. In the early days of Swift, we had a closure syntax that was very similar to traditional Javascript, func (arg: Type, arg: Type) -> Return { ... }
. While this is nice and regular syntax, it is of course also very bulky and awkward if you're trying to support expressive functional APIs, such as map
/filter
on collections, or if you want libraries to be able to provide closure-based APIs that feel like extensions of the language. Our earliest adopters at Apple complained about this, and mandated that we support Ruby-style trailing closure syntax. This is tricky to fit into a C-style syntax like Swift's, and we tried many different iterations, including literally Ruby's {|args| }
syntax, but many of them suffered from ambiguities or simply distaste and revolt from our early adopters. We wanted something that still looked like other parts of the language, but which could be parsed unambiguously and could span the breadth of use cases from a fully explicit function signature to extremely compact. We had already taken in
as a keyword, we couldn't use ->
like Java does because it's already used to denote the return type, and we were concerned that using =>
like C# would be too visually confusing. in
made xs.map { x in f(x) }
look vaguely like for x in xs { f(x) }
, and people hated it less than the alternatives.
106 Likes