I'm a huge fan of Swift's trailing closure syntax. It is much nicer than moving the closing ) to the end of the closure. This is not just an aesthetic difference: the closing ) signals to a reader that the argument list is complete with the closure. Without trailing closure syntax readers have to glance to the end of the closure to know if additional arguments are provided. This syntax is especially useful when the closure has several lines, as is common in callbacks.
Continuing the recent trend of discussing EDSLs, we should provide similar syntactic sugar for collection literals when the last argument in a parameter list is ExpressibleByArrayLiteral or ExpressibleByDictionaryLiteral. This sugar will be especially useful for DSLs that model trees and therefore have "children" of some kind.
It still conflicts. The following is valid Swift today, it contains your example verbatim at the bottom, and it calls the other foo function (which returns a Foo) then subscripts the result:
struct Foo {
subscript(_ s: String...) -> Int {
return s.count
}
}
func foo() -> Foo { return Foo() }
func foo(ints: [String]) { }
foo() [ // Calls the ()->Foo overload and subscripts the result
"hello",
"world"
]
Of course a variadic is possible. Unfortunately it requires the closing parentheses to be placed after the subview list. This isn't so bad. But it would be nice if we had the option to move the literal outside the call like we do with closures. Unfortunately that won't work - as was pointed out already it conflicts with subscript syntax.