SE-0279: Multiple Trailing Closures

Borrowing the with function from @Erica_Sadun's old pitch (with some hypothetical and explicit self rebind), combined with the idea I presented above to create a more appealing usage example:

let label = with {
  UILabel(frame: someFrame)
  update: { [self = $0] in
    textAlignment = .center
    font = UIFont(name: "DnealianManuscript", size: 72)
    text = questionText
    numberOfLines = 0
    mainView.addSubview(self)
  }
}

Honestly, this reads extremely good to me.


And yes I know you could also write it like so:

let label = with(
  UILabel(frame: someFrame),
  update: { [self = $0] in
    textAlignment = .center
    font = UIFont(name: "DnealianManuscript", size: 72)
    text = questionText
    numberOfLines = 0
    mainView.addSubview(self)
  }
)

And yes in this example the difference is really minor, but the general feature from my point of view would allow you to split the parameter list into a multi line trailing closure like parameter list at any point, which is by itself a great flexibility to have.

1 Like