2

As I am learning swift I find the concept of Lazy a bit confusing .

Lazy property is initialised when it is needed or accessed by the class instance

   class Employee
        {
            var name : String
            lazy var salary = Salary(Basic : 25000 ,HRA : 3000 , DA : 4000)    
            lazy var minExperience = 0 


     init(nameValue :String)
        {
            name = nameValue 

       } }

    var emp1 = Employee("John") // Here the minExperience and 
//salary will be nil as they are not assigned any space = 

//emp1.salary.storage = nil , emp1.minExperience.storage = nil 


// now access the minExperience

emp1.minExperience // using 1st time and this will return 0 !  emp1.salary.HRA // using 1st time and this will return 3000 !

So my questions are :

  1. as minExperience and salary are not assigned any storage space so till we access it where does it stores it values - 0 or 3000 ???

  2. one aspect of Lazy property is

    Lazy properties are useful when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete.

then why Xcode is forcing us to assign value at declaration time ??

  1. And as we have assigned a value to the Lazy property obviously we don't need to initialise it in our init method .
0

2 Answers 2

1

A lazy variable still allocates the memory it needs. It merely defers the execution of the initialization code.

That is the only difference with regular variables and, in both cases, the compiler needs a value to assign ( initially or upon first reference ).

in your example,

salary seems to be a good candidate for a lazy var because it requires a function to obtain the initial value and, presumably, that function is either not ready to operate when you create the Employee object or too costly to execute systematically (and could wait for actual use of the salary variable)

on the other hand, minExperience has no calculation for its initial value and does not benefit at all from being lazy.

If we didn't have lazy variables, we could obtain the same result using an internal variable and a computed property:

 internal var _salary : Salary! = nil
 var salary:Salary
 {  
    get {
           if _salary == nil
           { _salary = Salary(Basic : 25000 ,HRA : 3000 , DA : 4000) }
           return _salary 
        }

    set { _salary = newValue }
 }

This illustrates (approximatively) what the compiler is doing with a lazy var.

The result would be the same but requires much more code.

Sign up to request clarification or add additional context in comments.

2 Comments

Its a different thing that we can do the same thing with the computed properties and function. & Salary is just an example. We can have any complex class. So the que is where does it store the value "25000" , "3000" , "4000" till the initialisation?
The delayed initialization is like a function so these values are stored In the compiled code itself like any other constants you would put inside a function or function call.
0
  1. The class does not store lazy properties values. Instead, it only initialises lazy properties when you first refer to it:

A lazy stored property is a property whose initial value is not calculated until the first time it is used.

  1. You need to assign a value at declaration time because the compiler needs to know how to initialise the lazy property when time comes. But, if you want to assign values that depends on instance (self) or other objects, you can also define a closure, where you can access self and therefore outside factors that will than be known since the closure is called after instance's initialisation.

For instance, in your example you could do:

class Employee {
   var name : String
   lazy var minExperience = 0
   lazy var salary:Salary =  { [unowned self] in
       if self.name.characters.count > 0 {
           return Salary(Basic : 25000 ,HRA : 3000 , DA : 4000)
       }
       else {
           return Salary(Basic : 0 ,HRA : 0 , DA : 0)
       }
   }()

   init(nameValue :String) {
       name = nameValue
   } 
}

The fact that salary is a lazy property means that you can refer to self within the closure, because the lazy property will not be accessed until after initialisation has been completed and self is known to exist.

the [unowned self] in added to prevent leaks.

  1. You are right, we don't need to initialise lazy properties in the init method because this is the point of lazy properties, we don't know their values upon initialisation or wan't to initialise in initialisation class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.