SE-0279: Multiple Trailing Closures

Here is wild idea to the community and the proposal authors. I‘d appreciate any feedback.

In SwiftUI we have multiple cases where we use ViewBuilder function builder to stack some views.

VStack {
  Text("a")
  Text("b")
  Text("c")
  Text("d")
}

Some of us are getting used to this kind of DSL usage and it starts to feel natural.

So here is my question: WHAT IF we generalized the idea from this proposal not only to use it for trailing closures but for all init/func parameters which we then would stack in a similar way as shown above with VStack?

Example:

// before
transition(with: view, duration: 2.0,
  animations: {
    ...
  },
  completion: {
    ...
  }
)

// after
transition {
  with: view
  duration: 2.0
  animations: {
    ...
  }
  completion: {
    ...
  }
}
func foo(_: Int, _: String, flag: Bool, closure: (Int) -> Void) {}

// old forms
foo(42, "swift", flag: true, closure: { print($0) })

foo(42, "swift", flag: true) { print($0) }

// new possible forms
foo(42, "swift", flag: true) {
  closure: { print($0) }
}

foo(42, "swift") {
  flag: true
  closure: { print($0) }
}

foo(42) {
  "swift"
  flag: true
  closure: { print($0) }
}

foo {
  42
  "swift"
  flag: true
  closure: { print($0) }
}

This would be another DSL-ish style syntax we are already somehow familiar with.

2 Likes