# Possible? Use compiler synthesized init to initialize @ViewBuilder var in a SwiftUI View?

**URL:** <https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492>\
**Category:** Using Swift\
**Tags:** swiftui\
**Created:** [April 19, 2020, 3:51am UTC](https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492 "2020-04-19T03:51:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [April 19, 2020, 3:51am UTC](https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492/1 "2020-04-19T03:51:40Z")

</div>

So I wanted to avoid hand write init for this:

```swift
struct SimpleView<Content: View>: View {
    
    //
    // too many properties here, prefer to use compiler synthesized init...
    //
    
    // but how to deal with this? Marking this @ViewBuilder result in compile error:
    // Error: Function builder attribute 'ViewBuilder' can only be applied to a property if it defines a getter
    // @ViewBuilder
    let content: () -> Content
    
    // Is there anyway to skip writing this and use the compiler synthesisize init for content?
    init(@ViewBuilder content: @escaping () -> Content) {
        //
        // here would be lots of boiler plate properties init to write
        //
        self.content = content
    }
    
    var body: some View {
        content()
    }
}

```

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 19, 2020, 4:11am UTC](https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492/2 "2020-04-19T04:11:12Z")

</div>

I don't think there's anyway around it. As is, you'd need to write it out.

PS

I think most other views, such as [`Button`](https://developer.apple.com/documentation/swiftui/button/3283501-init) uses non-escaping version. So I'd suggest that you follow suits. In which case you'd need to invoke the call within `init` and save the data.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [April 19, 2020, 4:26am UTC](https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492/3 "2020-04-19T04:26:47Z")

</div>

> [@Lantua](#):
>
> uses non-escaping version

Oh 😲 I didn't get the memo. All the examples I see all do non-escape...I thought they were just oversight. Wonder what different does this make? Calling it in `init` vs. in `body`...hmmm 🤔

Either way, can't use compiler synthesized init then...seem if there is `@autoclosure`, seem there should be something like the inverse of it.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 19, 2020, 4:35am UTC](https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492/4 "2020-04-19T04:35:13Z")

</div>

Well, one could always pitch some...

Regarding the difference, there isn't much if you use it once. If you use it a few times, the compiler may have hard time proving that the function is pure (which I believe it would be for your usage). So it has no choice but to actually call it a few times. Storing a closure can also lead to some reference cycle shenanigan if you're not careful. Though to be fair, it could happen to any type that capture references.

> [@young](#):
>
> All the examples I see all do non-escape

Do you mean they all do escape?

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [April 19, 2020, 4:39am UTC](https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492/5 "2020-04-19T04:39:53Z")

</div>

> [@Lantua](#):
>
> Do you mean they all do escape?

No, they all do as you said, non-escape. I thought doing @escaping is better, guess not 🤩

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 19, 2020, 4:50am UTC](https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492/6 "2020-04-19T04:50:14Z")

</div>

With all that said, it's not that _escape == bad_, _non-escape == good_. If the function is sensitive to when you call it, you may want to escape it to call later.

Non-escaping closure is more appropriate here because you expect that the function is pure and side-effect free, whether you call it now or later, the result is the same. You also reduce the risk of reference cycle I mentioned earlier. One downside is that, instead of storing a closure (and captured variable), you store `Content` which can be potentially large.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [February 9, 2021, 1:27am UTC](https://forums.swift.org/t/possible-use-compiler-synthesized-init-to-initialize-viewbuilder-var-in-a-swiftui-view/35492/7 "2021-02-09T01:27:04Z")

</div>

[This is possible now in Swift 5.4](https://forums.swift.org/t/initializable-when-will-it-be-implemented/44570/3)
