I have a very simple project. It is Command Line Tool written on Swift 3.0 using Xcode 8.0. This program is:
import Foundation
func aaa() {
print(a)
}
let a = "a"
aaa()
This is working perfectly well and printing "a" in console, but lets do this program more complex:
import Foundation
func aaa() {
print(a)
print(b)
}
let a = "a"
let b = "b"
aaa()
And line
print(b)
is marked with error
Use of unresolved identifier 'b'
We can make even easier:
import Foundation
func aaa() {
print(a)
}
aaa()
let a = "a"
And again, line
print(a)
is marked with error
Use of unresolved identifier 'a'
I am not newbie and I undertand that I can easily fix this error like putting all variables in the beginning of the program. Question is: why is it happening? I thought each file with extension .swift, it is a class and I can put variable and functions, call functions in any order (all variables and constants would be global)... And one last thing, I don't have ability to test this on Swift 2.2, but I don't remember I faced this bug before, so can it be a error of Swift 3.0 compiler? Thank you for any answer!