0

Consider the following:

func test() -> Bool {
    for count in 0..<100 {
        return true 
    }
}

Missing return in a function expected to return 'Bool'. I do not see a case where the function will not return 'true', yet the code will not compile. Thoughts?

  • 1
    Usually you iterate the entire loop or you `return` on a condition or you `break` or `continue`, but a loop which returns immediately is completely pointless. The compiler never expects such a behavior. Instead it does expect a `return` value **after** the loop – vadian Dec 19 '21 at 11:04
  • Do you really expect it to return true 100 times? It will simply just return true on the first iteration, and as @vadian already mentioned you need to return a value after the loop. The compiler doesn't "know" that there is no chance of not returning a value. – Leo Dabus Dec 19 '21 at 14:01
  • It's an interesting edge case, but is completely covered by the duplicate. – matt Dec 19 '21 at 14:16

0 Answers0