# Stored property cannot have covariant 'Self' type

**URL:** <https://forums.swift.org/t/stored-property-cannot-have-covariant-self-type/66192>\
**Category:** Using Swift\
**Tags:** generics\
**Created:** [July 17, 2023, 7:23am UTC](https://forums.swift.org/t/stored-property-cannot-have-covariant-self-type/66192 "2023-07-17T07:23:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [July 17, 2023, 7:23am UTC](https://forums.swift.org/t/stored-property-cannot-have-covariant-self-type/66192/1 "2023-07-17T07:23:04Z")

</div>

While tinkering 🙂 with @David_Smith's excellent example [ThreadsafeCounter](https://forums.swift.org/t/deadlock-when-using-dispatchqueue-from-swift-task/66058/31), I got this error: `Stored property cannot have covariant 'Self' type.`

```swift
import os

@main
enum CounterTest {
    static func main () async {
        let counter = ThreadsafeCounter ()
        
        let N = 10 * 1024 * 1024
        for _ in 0..<N {
            Task.detached {
                counter.increment ()
            }
        }
        while counter.value < N {
            counter.decrement()
            counter.increment()
        }
        print (N, counter.value)
        assert (N == counter.value)
    }
}

```

```swift
final class ThreadsafeCounter {
    private (set) var value: Int = 0
    
    // var protectedCounter : OSAllocatedUnfairLock <ThreadsafeCounter>?
    var protectedCounter : OSAllocatedUnfairLock <Self>?
        // ^^^ Error: `Stored property cannot have covariant 'Self' type`

    init () {
        self.protectedCounter = OSAllocatedUnfairLock (initialState: self)
    }
    
    func increment() {
        protectedCounter?.withLock {$0.value += 1}
    }

    func decrement () {
        protectedCounter?.withLock {$0.value -= 1}
    }
}

```

However, the error disappears if I substitute `ThreadsafeCounter` for `Self`.

Can anyone shed some light on this please?

Thank you.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [July 17, 2023, 7:26am UTC](https://forums.swift.org/t/stored-property-cannot-have-covariant-self-type/66192/2 "2023-07-17T07:26:39Z")

</div>

FWIW that example was typed directly into the forum and has not been tested at all, caveat emptor 🙂

For an actual threadsafe counter I would probably use the Atomics package

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [July 17, 2023, 1:05pm UTC](https://forums.swift.org/t/stored-property-cannot-have-covariant-self-type/66192/3 "2023-07-17T13:05:20Z")

</div>

Tangentially related:

```swift
struct S {
    func foo() {
        let s: Self = self // ✅
    }
}

final class C {
    func foo() {
        let s: Self = self // 🛑 Cannot convert value of type 'C' to specified type 'Self'
    }
}

```

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [July 17, 2023, 4:37pm UTC](https://forums.swift.org/t/stored-property-cannot-have-covariant-self-type/66192/4 "2023-07-17T16:37:14Z")

</div>

> [@ibex](#):
>
> Can anyone shed some light on this please?

[Stored variable of Self type (especially when subclassing)](https://stackoverflow.com/questions/63964975/stored-variable-of-self-type-especially-when-subclassing) helps to explain this.

I suspect this is an oversight in the Swift compiler - as far as I can tell, the fact that `Self` (`ThreadsafeCounter`) is _final_ should mean that there are no covariance problems. Only if it were non-final would it open the door to covariance problems like those shown in the StackOverflow answers.
