There are two approaches to nested functions/closures:
As you have it:
func test() -> Bool {
func test1() -> Bool {
return true
}
func test2() -> Bool {
return true
}
func test3() -> Bool {
return true
}
return test1() && test2() && test3()
}
Or saving it to closures and calling them:
func test() -> Bool {
let test1 = { () -> Bool in
return true
}
let test2 = { () -> Bool in
return true
}
let test3 = { () -> Bool in
return true
}
return test1() && test2() && test3()
}
The difference is really small and it comes down to your own preference but straight from documentation:
Nested functions are closures that have a name and can capture values from their enclosing function.
and
Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context.
Either way, both cases have to be called with () at the end of the name to perform their action, otherwise you're returning a function and not its evaluation. Your return test1 would be only valid in this case:
func test() -> (() -> Bool) {
func test1() -> Bool {
return true
}
return test1
}
where your function's return type is a function that returns Bool after you evaluate it. You want to evaluate though and return the Bool value combined with logical AND statements. Thus, you have to evaluate each function to get each function's Bool value to combine it and return the final value.