# How could I create a property wrapper for a struct's memberwise init with projectedValue?

**URL:** <https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508>\
**Category:** Using Swift\
**Created:** [February 22, 2022, 9:37am UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508 "2022-02-22T09:37:19Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 9:37am UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/1 "2022-02-22T09:37:19Z")

</div>

when playing with swiftUI, I can initialise using the projectedValue on memberwise init method.  
But I want to know how it works? how do I write this kind of property wrapper?

```swift
    struct Something {
        @Binding var val1: Int
        @Binding var val2: Int
    }
    
    func foo() {
        @State var val1 = 0
        @State var val2 = 1
        let something = Something(val1: $val1, val2: $val2)
    }

```

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [February 22, 2022, 11:40am UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/2 "2022-02-22T11:40:58Z")

</div>

Here's how to explicitly declare the member-wise initializer:

```swift
struct Something {
    
    @Binding var val1: Int
    @Binding var val2: Int
    
    init(val1: Binding<Int>, val2: Binding<Int>) {
        self._val1 = val1
        self._val2 = val2
    }
    
}

```

It seems to me that you're trying to ask another question as well, but I'm not sure what. Please try to rephrase it.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 11:46am UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/3 "2022-02-22T11:46:17Z")

</div>

> [@carlhung](#):
>
> ```swift
> struct Something {
> @Binding var val1: Int
> @Binding var val2: Int
> }
>     
> func foo() {
> @State var val1 = 0
> @State var val2 = 1
> let something = Something(val1: $val1, val2: $val2)
> }
> 
> ```

the code I posted above, if you paste on a project using swiftUI. it will work. and the `struct` doesn't request writing a new init method which is `init(val1: Binding<Int>, val2: Binding<Int>)`. but struct comes with two default init methods of `init(val1: Binding<Int>, val2: Binding<Int>)` and `init(val1: Int, val2: Int)`

I think the struct comes with two default init methods. due to the property wrapper.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [February 22, 2022, 11:54am UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/4 "2022-02-22T11:54:48Z")

</div>

The rules for how member-wise initializers are synthesized on types with property wrappers are complicated. In your case, the compiler does _not_ generate `init(val1: Int, val2: Int)` for `Something`. This is because `Binding` does not have an `init(wrappedValue)`. See [SE-0258](https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md#memberwise-initializers).

Also, was there supposed to be a question in your post?

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 12:00pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/5 "2022-02-22T12:00:16Z")

</div>

> [@Peter-Schorn](#):
>
> r does _not_ gen

you are right. I just realised it doesn't come with `init(val1: Int, val2: Int)`.

so how do I create a property wrapper that can make struct to create a default init with the projectedValue?

---

<div class="post-metadata">

**Author:** ![mattcurtis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattcurtis/32/18493_2.png) [@mattcurtis](https://forums.swift.org/u/mattcurtis)\
**Post date:** [February 22, 2022, 12:21pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/6 "2022-02-22T12:21:18Z")

</div>

The feature you're asking about is not supported for memberwise initializers yet. [I asked a while back if this could be supported in a thread here](https://forums.swift.org/t/does-the-new-swift-5-5-init-projectedvalue-functionality-not-work-with-synthesized-memberwise-initializers/51232), which [escalated to a pitch](https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049) but has stalled since.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 12:23pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/7 "2022-02-22T12:23:05Z")

</div>

> [@mattcurtis](#):
>
> sking about is not sup

I had read it. weird. it works on method with property wrapper parameter. but not initialiser.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 12:37pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/8 "2022-02-22T12:37:44Z")

</div>

but why would swiftUI work?

---

<div class="post-metadata">

**Author:** ![mattcurtis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattcurtis/32/18493_2.png) [@mattcurtis](https://forums.swift.org/u/mattcurtis)\
**Post date:** [February 22, 2022, 12:40pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/9 "2022-02-22T12:40:50Z")

</div>

What you're seeing is the behavior where if a property wrapper lacks an `init(wrappedValue:)` initializer, the memberwise initializer will use the property wrapper itself as the value.

Say you have a view:

```swift
struct MyView {

    @Binding var someValue: Int

}

```

Since `Binding` has no `init(wrappedValue:)` initializer, the generated memberwise initializer becomes:

```swift
init(someValue: Binding<Int>) { ... }

```

Further indication that your example and a projected-value initializer are two different things is the fact that in your example, you don't need to prefix the parameter with `$`, which the "initialize a property wrapper using its projectedValue" support in functions requires.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [February 22, 2022, 12:47pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/10 "2022-02-22T12:47:26Z")

</div>

> [@carlhung](#):
>
> so how do I create a property wrapper that can make struct to create a default init with the projectedValue?

The projected value of what?

> [@carlhung](#):
>
> ```swift
> @State var val1 = 0
> @State var val2 = 1
> 
> ```

`val1` and `val2` are `@State<Int>` properties, so the type of their projected value (accessed using `$val1` and `$val2`, repsectively) is `Binding<Int>` (see the [documentation](https://developer.apple.com/documentation/swiftui/state/projectedvalue)).

> [@carlhung](#):
>
> `let something = Something(val1: $val1, val2: $val2)`

The type of both of the parameters of this initializer is `Binding<Int>` which is the same type as that of `$val1` and `$val2`, so that's why this code works.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 12:49pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/11 "2022-02-22T12:49:18Z")

</div>

my question is how to create this kind of property wrapper that can be generated by struct to use the projectedValue as default member-wise initialiser.  
I said it from the beginning. `how do I create this kind of property wrapper?`

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [February 22, 2022, 12:59pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/12 "2022-02-22T12:59:39Z")

</div>

> [@carlhung](#):
>
> my question is how to create this kind of property wrapper that can be generated by struct to use the projectedValue as default member-wise initialiser.

This sentence is extremely confusing to me. What is " **this** kind of property wrapper"? What does "this" refer to? What does it mean for a property wrapper to be "generated by [the] struct"? structs don't generate property wrappers. Which `projectedValue` are you referring to?

Are you asking about how to get the compiler to automatically synthesize a particular initializer for a struct that contains property wrapper instance properties? If so, please create an example in which you manually define the initializer that you want to compiler to synthesize so that I can understand what you're referring to.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 1:02pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/13 "2022-02-22T13:02:30Z")

</div>

I am asking how to create a property wrapper that `the compiler to automatically synthesize a particular initializer for a struct`. I had the example above. if I know how to create. I won't ask here.

seems like @mattcurtis know what I am saying.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [February 22, 2022, 1:05pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/14 "2022-02-22T13:05:57Z")

</div>

Can you provide the declaration for the initializer that you want the compiler to synthesize? That would be very helpful to me.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 1:10pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/15 "2022-02-22T13:10:44Z")

</div>

> [@Peter-Schorn](#):
>
> e declaration for the initializer that you w

I thought the example above is obvious enough.

```swift
struct Something {
    @Binding var val1: Int
    @Binding var val2: Int
    
    // implicitly generated.
    init(val1: Binding<Int>, val2: Binding<Int>) {
        self._val1 = val1
        self._val2 = val2
    }
}

```

but I am asking for how to create the property wrapper.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [February 22, 2022, 1:17pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/16 "2022-02-22T13:17:19Z")

</div>

What does "the property wrapper" refer to? Are you asking about how to create a property wrapper or about synthesized initializers? You're just making me more confused.

Is there really no other way you can phrase your question? Are there not any code examples you can create that would be helpful?

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [February 22, 2022, 1:23pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/17 "2022-02-22T13:23:37Z")

</div>

just forget.@mattcurtis answered my question. and I successfully made it.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [February 22, 2022, 1:37pm UTC](https://forums.swift.org/t/how-could-i-create-a-property-wrapper-for-a-structs-memberwise-init-with-projectedvalue/55508/18 "2022-02-22T13:37:34Z")

</div>

Can you show me what you made?
