Requiring an associated type to be a result builder in Swift

47 views Asked by At

Is there a way to create a protocol with a result builder as an associated type — for instance, to automatically create an initializer that takes a result builder closure?

Right now, I'm trying to do something like this:

public protocol ResultBuildable {
    @resultBuilder associatedtype Builder
    
    init(@Builder builder: () -> Self)
}

But the compiler doesn't like it very much:

'@resultBuilder' attribute cannot be applied to this declaration

(There's also Unknown attribute 'Builder', but that's dependent on the previous error.)

0

There are 0 answers