# How to return the runtime Self from a method like createWrapper() -\> Wrapper\<Self\>

**URL:** <https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317>\
**Category:** Using Swift\
**Tags:** generics\
**Created:** [January 7, 2020, 4:17pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317 "2020-01-07T16:17:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SubjugaterCephalopod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/subjugatercephalopod/32/9536_2.png) [@SubjugaterCephalopod](https://forums.swift.org/u/SubjugaterCephalopod)\
**Post date:** [January 7, 2020, 4:17pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317/1 "2020-01-07T16:17:50Z")

</div>

I feel like this may be something too obvious that I just can't see at the moment:

```swift
class Wrapper<T> {}

class Foo {
    // ❗️ 'Self' is only available in a protocol or as the result of a method in a class; did you mean 'Foo'?
    func createWrapper() -> Wrapper<Self> {
        return Wrapper<Self>()
    }
}

```

I want this function to be valid for all subclasses of `Foo`, but how?

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [January 7, 2020, 4:28pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317/2 "2020-01-07T16:28:10Z")

</div>

You should get a better error on master: `covariant 'Self' can only appear at the top level of method result type`

---

<div class="post-metadata">

**Author:** ![ddddxxx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ddddxxx/32/3237_2.png) [@ddddxxx](https://forums.swift.org/u/ddddxxx)\
**Post date:** [January 7, 2020, 4:31pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317/3 "2020-01-07T16:31:22Z")

</div>

like this?

```swift
class Wrapper<T> {}

protocol Wrappable {}

extension Wrappable {
    func createWrapper() -> Wrapper<Self> {
        return Wrapper<Self>()
    }
}

class Foo: Wrappable {}

Foo().createWrapper()

```

---

<div class="post-metadata">

**Author:** ![SubjugaterCephalopod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/subjugatercephalopod/32/9536_2.png) [@SubjugaterCephalopod](https://forums.swift.org/u/SubjugaterCephalopod)\
**Post date:** [January 7, 2020, 4:59pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317/4 "2020-01-07T16:59:32Z")

</div>

Thanks! @ddddxxx

A little hacky, with the empty protocol and all.

---

<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:** [January 7, 2020, 5:34pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317/5 "2020-01-07T17:34:03Z")

</div>

Here is some more material to learn from. `Self` in protocols means one things while `Self` on classes means something else: [Is it just me or did we end up having partly `StaticSelf` in Swift? - #5 by Slava\_Pestov](https://forums.swift.org/t/is-it-just-me-or-did-we-end-up-having-partly-staticself-in-swift/22752/5)

---

<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:** [January 7, 2020, 11:52pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317/6 "2020-01-07T23:52:54Z")

</div>

You can't do this because inside a class, `Self` is a shorthand for the runtime type of `self`, and not just an alternate spelling for the name of the class that lexically contains the definition.

If you could write `Self` there, it would introduce unsoundness into the typing rules. For example, suppose you have a subclass `Bar`:

```swift
class Bar : Foo {}

```

Now consider this code:

```swift
let bar = Bar()
let result1 = bar.createWrapper()

let foo = (bar as Foo)
let result2 = foo.createWrapper()

```

The type of result1 is `Wrapper<Bar>`, but the type of result2 is `Wrapper<Foo>`. These are two unrelated types.

---

<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:** [January 7, 2020, 11:53pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317/7 "2020-01-07T23:53:44Z")

</div>

I would suggest against using this protocol trick. It's actually equivalent to just declaring a class method returning `Wrapper<Foo>`. The `Self` in the protocol is a stand-in for the conforming class, not the runtime type of the `self` value. So using this idiom just obfuscates the intent of the code in my opinion.

---

<div class="post-metadata">

**Author:** ![DnV1eX](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dnv1ex/32/6601_2.png) [@DnV1eX](https://forums.swift.org/u/DnV1eX)\
**Post date:** [November 18, 2020, 6:02pm UTC](https://forums.swift.org/t/how-to-return-the-runtime-self-from-a-method-like-createwrapper-wrapper-self/32317/8 "2020-11-18T18:02:23Z")

</div>

It's not equivalent, because you lose the static type of the subclass. I would even say that this trick is becoming a kind of standard practice. For example, [here](https://github.com/DnV1eX/CovertSwiftObserver/blob/master/Sources/CovertSwiftObserver/ControlExtension.swift) I use it to observe UIControl events, so that the sender is statically not an abstract base class:

```swift
button.onEvent(.touchDown).run { (action: (sender: UIButton, event: UIEvent?)) in }

```

And [here](https://www.avanderlee.com/swift/custom-combine-publisher/#a-uicontrol-extension-to-easily-access-our-custom-publisher) they do about the same using Combine.
