# \[Pitch\] Refining Property-wrapper-related Initialization

**URL:** <https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049>\
**Category:** Pitches\
**Tags:** init, propertywrapper, projection\
**Created:** [September 14, 2021, 3:06pm UTC](https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049 "2021-09-14T15:06:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [September 14, 2021, 3:06pm UTC](https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049/1 "2021-09-14T15:06:22Z")

</div>

Hello Swift Community!

This thread posed the question:

```swift
struct MyStruct {
  @Wrapper var property = 0
}
MyStruct($property: Wrapper()) // ❌ Why isn't this allowed?

func acceptWrapped(@Wrapper property: Int = 0) { ... }
acceptWrapped($property: Wrapper()) // ✅ But this is?

```

So @amritpan, @Jumhyn and I co-authored the following pitch. Enjoy:

* * *

## Introduction

[SE 0258](https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md) introduced property wrappers and [SE 0293](https://github.com/apple/swift-evolution/blob/main/proposals/0293-extend-property-wrappers-to-function-and-closure-parameters.md#detailed-design) expanded them with function-like declarations. Today, property wrapper initialization exhibits inconsistencies due to its growing versatility. Specifically, memberwise initializers use complex, poorly documented rules and projection initialization remains limited. This proposal will simplify synthesized memberwise initialization for types with wrapped properties and extend projection value initialization to include global, type, and local wrapped properties.

Please read the rest [here](https://github.com/filip-sakel/swift-evolution/blob/NNNN-refining-property-wrapper-related-initialization/proposals/NNNN-refining-property-wrapper-related-initialization.md#motivation).

* * *

The source compatibility section outlines a novel "soft deprecation" approach, where your feedback would be especially valuable. Let us know what you think!

---

<div class="post-metadata">

**Author:** ![nikitamounier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikitamounier/32/24825_2.png) [@nikitamounier](https://forums.swift.org/u/nikitamounier)\
**Post date:** [September 14, 2021, 8:38pm UTC](https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049/2 "2021-09-14T20:38:45Z")

</div>

This definitely feels like a logical next step in the refining and generalisation of property wrapper initialization logic.

Would it make sense to also include the [Allow Property Wrappers with Multiple Arguments to Defer Initialization when wrappedValue is not Specified](https://forums.swift.org/t/allow-property-wrappers-with-multiple-arguments-to-defer-initialization-when-wrappedvalue-is-not-specified/38319) proposal in this proposal? I feel like it's also part of refining property wrapper initialization – I know that one of the authors of this proposal, @amritpan, is working on an implementation for it. I'm also curious on how these two proposals play with each other, or whether they might even conflict with each other.

Otherwise, +1 for me.

---

<div class="post-metadata">

**Author:** ![amritpan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/amritpan/32/39281_2.png) [@amritpan](https://forums.swift.org/u/amritpan)\
**Post date:** [September 14, 2021, 9:29pm UTC](https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049/3 "2021-09-14T21:29:13Z")

</div>

Great question! So far, in my cursory understanding, there are no conflicts between this pitch and the Defer Initialization for Multiple Arguments pitch. We'll be working on the implementation for this pitch next, will better understand whether there are any issues, and can consider their combination at that time.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [September 16, 2021, 3:35pm UTC](https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049/4 "2021-09-16T15:35:12Z")

</div>

> [@amritpan](#):
>
> So far, in my cursory understanding, there are no conflicts between this pitch and the Defer Initialization for Multiple Arguments pitch

Yeah, this is my understanding as well. Once the following from the linked pitch becomes legal code:

```swift
struct Model {
    @Argument(argument: "hello") var property: Int
}

```

this proposal would straightforwardly synthesize the memberwise init as:

```swift
init(@Argument(argument: "hello") property: Int) {
  self._property = _property
}

```

which would desugar to something like:

```swift
init(property: Int) {
  var _property: Argument = Argument(wrappedValue: property, argument: "hello")
  var property: Int {
    get { _property.wrappedValue }
    set { _property.wrappedValue = newValue }
  }

  self._property = _property
}

```

---

<div class="post-metadata">

**Author:** ![elychkouski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/elychkouski/32/30699_2.png) [@elychkouski](https://forums.swift.org/u/elychkouski)\
**Post date:** [July 7, 2023, 7:51pm UTC](https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049/5 "2023-07-07T19:51:26Z")

</div>

Hi guys!

May I ask what is actual status of this initiative?  
I'm working on the framework implementing the way of the API contract description and I'm really interested in the proposed changes!  
They would allow to do things like

```swift
struct FindArtistRequest {
    @Path("artist_name") var artistName: String
    @Query("app_id") var appId: String
}

```

instead of

```swift
struct FindArtistRequest {
    @Path("artist_name") var artistName: String = ""
    @Query("app_id") var appId: String = ""
}

```

and

```swift
struct LoginRequest {
    @JsonBody(encoder: .iso8601) var body: Credentials

    struct Credentials: Encodable {
        let email: String
        let password: String
    }
}

```

which is currently almost impossible!

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![elychkouski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/elychkouski/32/30699_2.png) [@elychkouski](https://forums.swift.org/u/elychkouski)\
**Post date:** [September 6, 2023, 11:46am UTC](https://forums.swift.org/t/pitch-refining-property-wrapper-related-initialization/52049/6 "2023-09-06T11:46:46Z")

</div>

@Jumhyn @amritpan Sorry for bothering you, but I'm really interested to bring this topic up. Is there any plans or roadmap regarding this small, but nice improvement?
