I tried two ways of initializing a lazy var, one works, the other gets a compiler error.
- OK: var maxDiscount = MaxDiscount(); maxDiscount.maxDiscountPercent
- ERROR: MaxDiscount().maxDiscountPercent If maxDiscountPercent was a stored property rather than a lazy var both usages would work.
Simplified example:
struct MaxDiscount {
lazy var maxDiscountPercent: Int = {
print("Lazy var maxDiscountPercent initialized")
return 256
}()
}
// initializes lazy var without an error
var maxDiscount = MaxDiscount()
print("Max percent discount: ", maxDiscount.maxDiscountPercent)
// initializes lazy var with error:
// "Cannot use mutating getter on immutable value: function call returns immutable value"
var maxDiscountPercent = MaxDiscount().maxDiscountPercent
print("Max percent discount: ", maxDiscountPercent)
It's not clear to me why one usage is mutable and the other immutable.