# 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:** 1\
**Showing post:** 7

<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 7, 2018, 6:54am UTC](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924/7 "2018-09-07T06:54:03Z")

</div>

> [@Joe\_Groff](#):
>
> ```swift
> class Y { 
> required init() {} 
> static func singletonInstance() -> Self { return self.init() } 
> convenience init(pref: Y) { 
> self = type(of: self).singletonInstance() 
> ^~~~
> } 
> }
> 
> ```

Is this a typo in the example, `pref` is never used?! Otherwise I don't understand the purpose of this example except that it shows an unambiguous `init(pref:)` as a factory init.

Other than that, Swift still does not allow the usage of `Self` in classes ([SE-0068](https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md) is still not implemented after years). In the given example I read `Self` as it would mean the same thing like if you had a protocol with `Self` that this class conforms to. I had a short discussion on twitter [[1]](https://twitter.com/slava_pestov/status/1029271129980985344) with @Slava_Pestov about the proposal a while ago. I must admit that I did not fully understand the technical issue with `Self`, but he said that we have to reconsider what `Self` would mean on classes. It seems that the reconsideration of `Self` touches this pitch since we need to clarify what the following code means:

```swift
extension Y {
  convenience init(other: Self) {
    self = other
  }
}

```

Or what does `Self` means in the related thread from the [1] discussion in case of a generic type parameter:

- [When is the right time to push SE-0068?](https://forums.swift.org/t/when-is-the-right-time-to-push-se-0068/15150)

* * *

Nevertheless I really like the general sense of this pitch especially if combined with SE-0068. Then I can finally refactor this pattern to something more naturally hidden:

```swift
// Before
protocol XibDesignable : AnyObject {}

extension XibDesignable where Self : UIView {
  static func instantiateFromXib() -> Self {
    let metatype = Self.self
    let bundle = Bundle(for: metatype)
    let nib = UINib(nibName: "\(metatype)", bundle: bundle)
    guard let view = nib.instantiate(withOwner: nil, options: nil).first as? Self else {
      fatalError("Could not load view from nib file.")
    }
    return view
  }
}

// After:
extension UIView {
  static func instantiateFromXib() -> Self {
    let metatype = Self.self
    let bundle = Bundle(for: metatype)
    let nib = UINib(nibName: "\(metatype)", bundle: bundle)
    guard let view = nib.instantiate(withOwner: nil, options: nil).first as? Self else {
      fatalError("Could not load view from nib file.")
    }
    return view
  }
}

final class View : UIView {
  convenience init() {
    self = Self.instantiateFromXib()
  }
}

```

---

_[View the full topic](https://forums.swift.org/t/allow-self-x-in-class-convenience-initializers/15924)._
