Hello,
Has there been a proposal for readonly lazy vars?
Here's a contrived example.
final class Object {
private let x = 0
private(set) lazy var y = x + 1
init() {
x = 2 // how to avoid this being possible?
}
}
Declaring y as private let y = x + 1
displays Cannot use instance member 'x' within property initializer; property initializers run before 'self' is available
.
Declaring y as 'lazy' as private lazy let y = x + 1
displays 'lazy' cannot be used on a let.
The best approach so far is to declare y as private(set) lazy var y = x + 1
. This has the advantage that at least the property is immutable outside of the instance. However, it is still mutable from within the class.
E.g.
private let x = 0
private(set) lazy var y = x + 1
init() {
y = 2
}
}
print(Object().y)
// 2
in this example, how can the line y =2
become a compile error? It must be an omission from the Swift language, that lays great value in immutable properties. If it needs to be said at all, immutable properties make for code that is easier to reason able, due to the reduction of mutable state. That can help engineers introduce fewer bugs, someone everyone should support.