`self` within initializer expression of stored type property is `unresolved identifier`

As far as I know

Stored type properties are lazily initialized on their first access.
(ref)

So why can't we refer to self within initializer expression of a stored type property?

class Foo {
    static let name = String(describing: self) // ERROR: Use of unresolved identifier 'self'
}
4 Likes

Because in a static context there is no self.

I don't think so. Look at this part of the documentation:

Type Properties
...
You can also define properties that belong to the type itself, not to any one instance of that type. There will only ever be one copy of these properties, no matter how many instances of that type you create. These kinds of properties are called type properties.

Also, replacing the stored type property with a computed type property solve the problem:

class Foo {
    static let name: String {
        return String(describing: self)
   }
}

But the question is why self is not available within initializer expression.

Terms of Service

Privacy Policy

Cookie Policy