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

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?

    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)
    }

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

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.

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.

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.

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

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?

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, which escalated to a pitch but has stalled since.

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

but why would swiftUI work?

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:

struct MyView {

    @Binding var someValue: Int

}

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

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.

The projected value of what?

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).

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.

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?

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.

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.

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

I thought the example above is obvious enough.

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.

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?

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

Can you show me what you made?