lazy now works in local contexts
lazy now works in local contexts êŽë š
Available from Swift 5.5
The lazy
keyword has always allowed us to write stored properties that are only calculated when first used, but from Swift 5.5 onwards we can use lazy
locally inside a function to create values that work similarly.
This code demonstrates local lazy
in action:
func printGreeting(to: String) -> String {
print("In printGreeting()")
return "Hello, \(to)"
}
func lazyTest() {
print("Before lazy")
lazy var greeting = printGreeting(to: "Paul")
print("After lazy")
print(greeting)
}
lazyTest()
When that runs youâll see âBefore lazyâ and âAfter lazyâ printed first, followed by âIn printGreeting()â then âHello, Paulâ â Swift only runs the printGreeting(to:)
code when its result is accessed on the print(greeting)
line.
In practice, this feature is going to be really helpful as a way of selectively running code when you have conditions in place: you can prepare the result of some work lazily, and only actual perform the work if itâs still needed later on.