Some types and protocols are strongly associated with a result builder (View and its @ViewBuilder from SwiftUI comes to mind, but there are several others).
In a View conforming type you need to remember to decorate your private → some View returning functions with @ViewBuilder or you'll get compilation errors if you introduce e.g. conditionals.
In the following code, the commenting in or our the line causes the code to compile, or not compile. It is almost always the intention of the user to use @ViewBuilder on this function:
struct MyView: View {
// ...
private func buttonRow() -> some View {
if condition {
HStack {
createButton("Foo", true, "fooo")
createButton("Yes", true, "yeees")
// createButton("Bar", false, "barrrr")
}
} else {
HStack {
createButton("Qux", true, "quiz?")
createButton("Frup", true, "frappapapapapp")
createButton("Boik", false, "boioioink")
}
}
}
}
Shold Swift have some mechanism of associating a result builder with a type or protocol, so that all of these syntaxes Just Do The Rigth Thing?
// auto-apply @HTMLBuilder to functions that return
// `some` protocols with associated result builder?
func foo() -> some HTML { ... }
// auto-apply @HTMLBuilder and infer return value when
// constructing values with a closure?
let html = HTML { ... }
These ideas are not mature, I'm not sure if we should just apply the attribute to the type (currently resultbuilders aren't applicable on types or extensions):
@HTMLBuilder
protocol HTML { ... }
Or maybe some associatedtype-magic?
I'm not sure. But Do you think the idea has merit?