Lazy Stored Properties
Sometimes a stored property’s value cannot be assigned immediately. The necessary information may be available, but computing the values of a property immediately would be costly in terms of memory or time. Or, perhaps a property depends on factors external to the type that will be unknown until after the instance is created. These circumstances call for lazy loading.
In terms of properties, lazy loading means that the calculation of the property’s value will not occur until the first time it is needed.
This delay defers computation of the property’s value until after the instance is initialized.
This means that lazy properties must be declared with var, because their values will change.
Create a new lazy ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access