# Why are properties that have Property Wrappers not included in the struct initializer?

**URL:** <https://forums.swift.org/t/why-are-properties-that-have-property-wrappers-not-included-in-the-struct-initializer/64063>\
**Category:** Using Swift\
**Tags:** swiftui\
**Created:** [March 29, 2023, 9:00pm UTC](https://forums.swift.org/t/why-are-properties-that-have-property-wrappers-not-included-in-the-struct-initializer/64063 "2023-03-29T21:00:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simanerush](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/simanerush/32/30785_2.png) [@simanerush](https://forums.swift.org/u/simanerush)\
**Post date:** [March 29, 2023, 9:00pm UTC](https://forums.swift.org/t/why-are-properties-that-have-property-wrappers-not-included-in-the-struct-initializer/64063/1 "2023-03-29T21:00:34Z")

</div>

I have a `View` struct that has two properties, both `private var`s, but one is a `@State.` Both have a default value:

```swift
struct MyView: View {
    private var x: Int = 0
    @State private var y = 0

    . . .
}

```

With this, I ran into the issue of being unable to initialize that struct because of the `x` variable despite it having the default variable. See the bug report: [`initializer is inaccessible due to 'private' protection level` error for structs with private variables with default values. · Issue #64738 · apple/swift · GitHub](https://github.com/apple/swift/issues/64738)

This led me to think that Property Wrappers are not included in the struct's initializer, and that's why they do not cause that error since they don't affect the initializer protection level.

I'm curious, why are Property Wrappers not included in the View initializer?

---

<div class="post-metadata">

**Author:** ![ksemianov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ksemianov/32/18723_2.png) [@ksemianov](https://forums.swift.org/u/ksemianov)\
**Post date:** [March 29, 2023, 9:55pm UTC](https://forums.swift.org/t/why-are-properties-that-have-property-wrappers-not-included-in-the-struct-initializer/64063/2 "2023-03-29T21:55:04Z")

</div>

Both `x` and `y` are not included in the automatically synthesized initializer, because the variables are declared `private`. But you can define a custom one.

```swift
struct MyView: View {
    private var x: Int = 0
    @State private var y = 0

    init(x: Int, y: Int) {
        self.x = x
        self.y = y
    }

    . . .
}

```

---

<div class="post-metadata">

**Author:** ![rayx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayx/32/12204_2.png) [@rayx](https://forums.swift.org/u/rayx)\
**Post date:** [March 30, 2023, 2:50am UTC](https://forums.swift.org/t/why-are-properties-that-have-property-wrappers-not-included-in-the-struct-initializer/64063/3 "2023-03-30T02:50:37Z")

</div>

FYI that I find a way to verify the signature of the _private_ synthesized `init()` by using static variable and Xcode autocomplete feature.

For the issue in the bug report, the synthesized `init()` includes `y`. It doesn't compile because the `init()` is private:

```swift
struct Struct {
    // this works
    static var shared = Struct(x: 1, y: 1)

    var x: Int
    private var y: Int = 0
}

```

When `y` is wrapped in a property wrapper, a similar experiment shows that synthesized `init()` includes `y` only if it doesn't have default value. Otherwise it doesn't (I'm also curious why).

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [March 30, 2023, 4:20am UTC](https://forums.swift.org/t/why-are-properties-that-have-property-wrappers-not-included-in-the-struct-initializer/64063/4 "2023-03-30T04:20:59Z")

</div>

Here’s the pull request: [Prevent less-visible properties with delegates from affecting memberwise initializer by DougGregor · Pull Request #24390 · apple/swift · GitHub](https://github.com/apple/swift/pull/24390)

Turns out we are just being on the defensive (until a proper proposal on improving memberwise `init` derivation comes forth).
