What is the right way to override lazy var in Swift 4.1? The following code work fine in swift 4.0 but since swift 4.1 I have warning on override, so I guess it'll be unavailable in swift 5
I used to:
class A {
lazy var myVar: String = {
return "A"
}()
}
class B: A {
override lazy var myVar: String = { // WARNING Cannot override with a stored property myVar
return "B"
}()
}
I guess this is something like this but its not sexy..
class A {
lazy var myVar: String = {
return createMyVar()
}()
func createMyVar() -> String {
return "A"
}
}
class B: A {
override func createMyVar() -> String {
return "B"
}
}
Although there's no technical reason why you cannot override a property with one that introduces storage (although it can raise ambiguities with observer overrides; see this Q&A for more info), Swift doesn't currently allow you to do so.
The fact that in 4.0 you could override a property with a lazy
property was unintended (as the override introduces storage), and so you'll get a warning in 4.1 and an error in Swift 5 mode in order to preserve source compatibility (implemented in #13304).
You can however achieve the same result with a forwarding computed property though:
class A {
lazy var myVar: String = "A"
}
class B : A {
// Note that this isn't a particulary compelling case for using 'lazy', as
// the initialiser expression is not expensive.
private lazy var _myVar: String = "B"
override var myVar: String {
get { return _myVar }
set { _myVar = newValue }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With