Reputation: 6524
True, that this case is bit contorted, but still I am curious to know if this is a potential bug in Swift2.0 compiler. Try this in your Xcode7 playground-
var global : Int
var factorial : (Int->Int)!
factorial = { x in
global = global + 1
print(global)
return x == 0 ? 1 : x * factorial(x - 1)
}
factorial(5)
Compiler happily compiles and even computes the factorial and increments the global too and even displays the value of the global within the closure when called. Now try printing the global after the call to factorial(5), and now compiler suddenly starts complaining that you are trying to use 'global' before being initialised!.
I think this a bug in the compiler somewhere and behaviour had to be consistent. But thought of consulting experts out there first!. Any thoughts?
Upvotes: 1
Views: 57
Reputation: 5694
Looks like a bug to me. Reproducible in both Swift 1.2 and 2.0 for me. File a radar, Apple will appreciate this.
Upvotes: 1