As common, many swift developers initialize lazy variables by lambda execution. But I can't understand why they use lambda when it is one line of computation. What is the different between the following code examples?
var x = 7
var y = 9
lazy var z = x * y
var x = 7
var y = 9
lazy var z: Int = {
return x * y
}()
I am new in swift, and from a naïve point of view the difference looks like that:
- the first one sample computes
x * y
immediately but initializes value by the lazy way - the second one sample computes and initializes by the lazy way.
Is it correct?
This question isn't duplication of What is the advantage of closure stored property Initialisation? because it is about lazy computation.