I would like you consider adding support for @ViewBuilder to be used with properties, so that the memberwise initializer Swift generates for structs automatically gains that function builder.
As an example, if I wanted to implement a custom SwiftUI VStack right now, I might start with this:
structCustomVStack<Content: View>: View {
let content: () -> Content
var body: some View {
VStack {
// custom stuff herecontent()
}
}
}
However, that doesn't support @ViewBuilder, and so this kind of code would not work:
CustomVStack {
Text("Hello")
Text("Hello")
}
To fix this, I need to define a custom initializer:
In this simple example it isn't a massive problem, but often that initializer has to copy in lots of values – it does exactly what the memberwise initializer did now just with @ViewBuilder for the single content property.
Ideally I'd like to be able to write something along these lines:
structCustomVStack<Content: View>: View {
@ViewBuilderlet content: () -> Content
var body: some View {
VStack {
// do stuffcontent()
}
}
}
Thank you!
The text was updated successfully, but these errors were encountered:
Maybe we can make this work by allowing users to add the function builder attribute on the type declaration i.e. let foo: @BarBuilder () -> T and then cloning it to the corresponding member-wise initializer parameter.
Additional Detail from JIRA
md5: 7cb2c7fde2a7f991ee63ebf387d335e3
Issue Description:
I would like you consider adding support for @ViewBuilder to be used with properties, so that the memberwise initializer Swift generates for structs automatically gains that function builder.
As an example, if I wanted to implement a custom SwiftUI VStack right now, I might start with this:
However, that doesn't support @ViewBuilder, and so this kind of code would not work:
To fix this, I need to define a custom initializer:
In this simple example it isn't a massive problem, but often that initializer has to copy in lots of values – it does exactly what the memberwise initializer did now just with @ViewBuilder for the single content property.
Ideally I'd like to be able to write something along these lines:
Thank you!
The text was updated successfully, but these errors were encountered: