SE-0348: buildPartialBlock for Result Builders

Thanks for the explanation. It seems like the result builder API is currently somewhat Frankensteined from the major use cases (SwiftUI and now Regex). I feel that adding a build expression variant to address this use case would only further fragment and complicate things.

Oh well, maybe with Swift 6 we will be able to move to a single, simpler model that can support all of these use cases (and hopefully more that we haven’t even thought of).

1 Like

I am very excited for this addition to the language. It is well motivated, well scoped, and adds significant power to result builders without adding significant difficulty of understanding.

The problem being addressed is very real (as demonstrated by the fact that it was prompted by an actual need which arose while implementing regex builders).

The proposal fits well with result builders as they now are and very conscientiously chooses names for the new APIs based on existing precedent in the standard library (which I agree also happen to be the most clear in this context).

I don't know of other languages with result builders and therefore can't compare.

I followed this proposal back in the pitch stage and felt similarly about it then, and I've reviewed the proposal text and all the discussion thus far.

7 Likes

Is this concept similar to a Haskell Monad? (Where Swift's buildPartialBlock is like Haskell's >>= operator, and the Swift DSL syntax is like Haskell's "do" syntax.) I wonder if there's anything in Haskell's experience with monads that would help this proposal, or steer towards or away from some semantics. (For example, Haskell eventually decided that monads were a special type of Applicative. Should there be a Swift equivalent of Applicative?)

There's an existing limitation in result builder inference due to its use of one-way constraints.

Towards the end of this thread, @Douglas_Gregor mentions that a theoretical buildFold operation could allow for this limitation to be relaxed. I was hoping buildPartialBlock would unlock some of the more fluent APIs described in the thread, but when I tested the snapshot this doesn't appear to be the case. Could such an enhancement be considered now?

6 Likes