Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Lazy var initialization error "Cannot use mutating getter on immutable value"

I tried two ways of initializing a lazy var, one works, the other gets a compiler error.

  1. OK: var maxDiscount = MaxDiscount(); maxDiscount.maxDiscountPercent
  2. 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you write var maxDiscount, you declare a variable (mutable). You would have a constant (immutable) with let maxDiscount = MaxDiscount().

Now with var maxDiscountPercent = MaxDiscount().maxDiscountPercent, MaxDiscount() is an anonymous constant and you can’t do everything on it.

Try let maxDiscount = MaxDiscount() and you will get the same type of error and an invitation to change let by var.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading