How will do-blocks work in the future?


A while back, I pitched the idea of using do-blocks to immediately invoke a closure, instead of putting (easily overlooked) empty parens after the block. So, these two lines would be equivalent:

let x: Int = { ... }()
let x: Int = do { ... }

At that time, someone suggested using something like this instead:

func iife<T>(_ closure: () -> T) -> T { closure() }

With that helper defined, this line becomes equivalent to the first two:

let x: Int = iife { ... }

I used this iife helper so often that I later asked if we could bikeshed a name people liked, then add it to the standard library, so it's available out the box (and to update the docs to use iife instead of trailing parens).

I was told an iife helper would be redundant soon, as Swift will be adding support for do-expressions (basically, what I originally wanted, but as part of a broader proposal). Naturally, I was stoked about that outcome, so was disappointed when I installed Swift 6, and do-expressions were not supported.

I've tried searching the forum (and Web, generally), and have seen the proposal for extending the do-grammar, but cannot figure out the plan for do-expressions as IIFEs.

Is there a proposal that would make do {x} an expression, equivalent to {x}()?

@Ben_Cohen has pitched it, yes. It has not matured to a proposal for review.


Ahh. I see. Thank you, @xwu - Much appreciated.