Sometimes, I want to use guard combined with let & where to simplify my code. But I wonder what's the priority of let and where. For example:
class Person {
func check() -> Bool? {
print("checking")
return nil
}
}
func test(dont: Bool) {
let person = Person()
guard let check = person.check() where dont else {
print("should not check")
return
}
print("result: \(check)")
}
test(false)
As you can see the console result, printed output are:
- checking
- should not check
For the condition of let check = person.check() where dont in guard <condition> else { } syntax, even the expression in where doesn't relate to the results of expression in let, Swift seems to execute let first then check where later. Sometimes in my code, let optional binding takes lots of calculation and where is only a simple condition without relying on let results, should I move the where out of guard? Or I'm wrong about the priority or let & where?