Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-13188] Please support @ViewBuilder for properties #55628

Closed
twostraws opened this issue Jul 9, 2020 · 3 comments · Fixed by #34097
Closed

[SR-13188] Please support @ViewBuilder for properties #55628

twostraws opened this issue Jul 9, 2020 · 3 comments · Fixed by #34097
Labels
compiler The Swift compiler in itself feature A feature request or implementation memberwise init Feature: Memberwise structure initializers result builders Feature: Result builders struct Feature → type declarations: Structure declarations swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented type checker Area → compiler: Semantic analysis

Comments

@twostraws
Copy link
Contributor

Previous ID SR-13188
Radar None
Original Reporter @twostraws
Type New Feature
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels New Feature, FunctionBuilder, TypeChecker
Assignee None
Priority Medium

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:

struct CustomVStack<Content: View>: View {
    let content: () -> Content

    var body: some View {
        VStack {
            // custom stuff here
            content()
        }
    }
}

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:

init(@ViewBuilder content: @escaping () -> Content) {
    self.content = content
}

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:

struct CustomVStack<Content: View>: View {
    @ViewBuilder let content: () -> Content

    var body: some View {
        VStack {
            // do stuff
            content()
        }
    }
}

Thank you!

@theblixguy
Copy link
Collaborator

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.

@DougGregor
Copy link
Member

Implementation in #34097 which is going through CI now

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation memberwise init Feature: Memberwise structure initializers struct Feature → type declarations: Structure declarations swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented and removed new feature labels Feb 24, 2023
@AnthonyLatsis
Copy link
Collaborator

Implemented as part of SE-0289

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation memberwise init Feature: Memberwise structure initializers result builders Feature: Result builders struct Feature → type declarations: Structure declarations swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented type checker Area → compiler: Semantic analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants