# Allow \`self = x\` in class convenience initializers

**URL:** <https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924>\
**Category:** Pitches\
**Created:** [September 6, 2018, 9:19pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924 "2018-09-06T21:19:33Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [September 8, 2018, 5:15am UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/21 "2018-09-08T05:15:53Z")

</div>

> [@Jon\_Hull](#):
>
> Is there a reason this is only for convenience initializers?

In a non-final class, a designated initializer can be called as part of a super.init delegation from a derived class. The "self" value is going to be a partially initialized instance of the derived class in this case, with the derived class's stored properties already initialized but nothing else. It wouldn't make sense to replace "self" with a fully initialized instance while it is in this state.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [September 14, 2018, 1:34am UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/22 "2018-09-14T01:34:33Z")

</div>

I have a prototype implementation here: [[awaiting evolution] Allow convenience initializers to reassign self. by jckarter · Pull Request #19311 · apple/swift · GitHub](https://github.com/apple/swift/pull/19311)

---

<div class="post-metadata">

**Author:** ![Philippe\_Hausler](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/philippe_hausler/32/22569_2.png) [@Philippe\_Hausler](https://forums.swift.org/u/Philippe_Hausler)\
**Post date:** [September 20, 2018, 8:28pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/23 "2018-09-20T20:28:21Z")

</div>

I am quite glad there is movement on this! It will definitely help swift-corelibs-foundation be not only more efficient but also work more in-line with the objc implementation for factory style initializers.

Per the designated initializer case it would be nice to allow this if and only if the type of self == the type that was allocated (e.g. a runtime check for the non-final case that Slava brought up). That change would allow SCLF to implement every case that should be a factory pattern.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [September 21, 2018, 3:58pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/24 "2018-09-21T15:58:00Z")

</div>

Supporting that would require some fancier control-flow-sensitive analysis than we current do for definite initialization, since we'd have to ensure that a `self =` assignment only occurred in a context where it was known that `Self == ConcreteClass`. Maybe if we a type-refining cast feature like [like what @grego proposes here](https://forums.swift.org/t/conditionally-further-constrain-a-generic-parameter-if-t-constrained/15425) we could confine self-assignments in a designated initializer to `if <Self == C>` condition blocks.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [September 22, 2018, 5:45am UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/25 "2018-09-22T05:45:00Z")

</div>

Slightly off-topic, but would it make sense to add a similar feature to Swift like `instancetype` from Objective-C. That type should bound the currently unconstrained `Self` to something that you just mentioned `Self == C`. This is really a missing feature for factory initializer in the context of designated initializers and it would also allow new and safe generic patterns to be expressed in Swift. What do you think @Joe_Groff?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [September 24, 2018, 4:42pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/26 "2018-09-24T16:42:33Z")

</div>

`Self` is `instancetype`. Factory initializers are interesting in that they do _not_ always return `instancetype`.

---

<div class="post-metadata">

**Author:** ![hartbit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hartbit/32/738_2.png) [@hartbit](https://forums.swift.org/u/hartbit)\
**Post date:** [January 5, 2019, 8:12am UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/27 "2019-01-05T08:12:17Z")

</div>

What's the update on this proposal? Anything blocking it?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [January 5, 2019, 5:34pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/28 "2019-01-05T17:34:59Z")

</div>

I haven't had time to finish the implementation. There are still a few issues with the implementation in [[awaiting evolution] Allow convenience initializers to reassign self. by jckarter · Pull Request #19311 · apple/swift · GitHub](https://github.com/apple/swift/pull/19311/files) that need to be fixed to get the tests passing.

---

<div class="post-metadata">

**Author:** ![Saklad5](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Saklad5](https://forums.swift.org/u/Saklad5)\
**Post date:** [January 11, 2019, 12:39pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/29 "2019-01-11T12:39:51Z")

</div>

This would be particularly helpful for classes that represent Core Data entities, as it would allow you to define initializers that use NSFetchRequests to retrieve information.

---

<div class="post-metadata">

**Author:** ![fredpi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fredpi/32/6927_2.png) [@fredpi](https://forums.swift.org/u/fredpi)\
**Post date:** [April 5, 2019, 10:50am UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/30 "2019-04-05T10:50:47Z")

</div>

I guess I'm way too impatient, but I'd love to see this feature so I was just wondering when it will be merged 😅

---

<div class="post-metadata">

**Author:** ![David\_Knothe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_knothe/32/2585_2.png) [@David\_Knothe](https://forums.swift.org/u/David_Knothe)\
**Post date:** [April 5, 2019, 10:54am UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/31 "2019-04-05T10:54:37Z")

</div>

Me too!

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 5, 2019, 3:29pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/32 "2019-04-05T15:29:16Z")

</div>

The implementation needs to be completed for the feature to be considered for review. I got a start in [Proposal: Allow convenience inits to assign `self` by jckarter · Pull Request #910 · apple/swift-evolution · GitHub](https://github.com/apple/swift-evolution/pull/910) but there is more work to be done.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 29, 2020, 1:30pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/33 "2020-09-29T13:30:53Z")

</div>

Just needed this one today 🙂

```swift
import Contacts

extension CNPostalAddress {
    convenience init(_ apiAddress: API.Address) {
        let address = CNMutablePostalAddress()
        address.city = ...
        self = address // Cannot assign to value: 'self' is immutable
    }
}

```

---

<div class="post-metadata">

**Author:** ![nh7a](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nh7a/32/1141_2.png) [@nh7a](https://forums.swift.org/u/nh7a)\
**Post date:** [November 4, 2022, 8:08pm UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/34 "2022-11-04T20:08:04Z")

</div>

Exactly why I searched and found this old thread today 🙃

[Previous page](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924.md?page=1)
