Nevin
1
I often write code with this pattern:
struct SomethingUseful {
static let initialSize: Int = 1
var size: Int = Self.initialSize
}
This works great, and provides a named constant for the initial value. However, occasionally SomethingUseful needs to be a reference type, so I declare it as a final class:
final class SomethingUseful {
static let initialSize: Int = 1
var size: Int = Self.initialSize
}
Unfortunately, that gives an error on the third line:
error: covariant 'Self' type cannot be referenced from a stored property initializer
This issue was raised on the forums back in 2020, and before that it was reported as a bug in 2019. Has there been any progress toward fixing it?
The workaround of writing out the full type name is fine, but it reduces readability.
• • •
For that matter, even in a struct, having to write Self.initialSize rather than just initialSize at the instance level also reduces readability. One could technically work around that by declaring the somewhat duplicative var initialSize: Int { Self.initialSize }, but that’s entirely boilerplate. Is there a better solution?
2 Likes