想知道是否可能对简单的if语句进行惰性求值。以下是一个示例,将打印“this is foo”和“this is bar”,但我真的希望它只打印第一个字符串:
func foo() {
println("this is foo")
}
func bar() {
println("this is bar")
}
func maybeFooOrBar(isFoo: Bool) {
let myFoo = foo()
let myBar = bar()
isFoo ? myFoo : myBar
}