[Pitch #2] Function builders

I was thinking in the line of property wrappers where the parens invoke a matching initialiser and omitting the parens invokes the “sensible initialiser”. For function builders specifically, @mybuilder would be shorthand for @mybuilder(), and a diagnostic would be reported if no zero-parameter initialiser is declared or accessible. The shorthand also makes all existing function builder uses (SwiftUI, etc.) source-compatible at the call-site.

I haven’t pondered about exact uses yet, but I presume there could be DSLs where the builder is configured for the specific container it’s being used for. I’ll try a (probably contrived) example:

struct Division<Content : Element> : Element {
    init(@ElementBuilder(containerStyle: .block) content: () -> Content) { … }
    // …
}

struct Heading<Content : Element> : Element {
    init(@ElementBuilder(containerStyle: .inline) content: () -> Content) { … }
    // …
}
1 Like