[swift-users] Can anyone please explain this behavior?

Marco S Hyman marc at snafu.org
Wed Sep 21 16:59:19 CDT 2016


> On Sep 21, 2016, at 2:22 PM, Jens Persson via swift-users <swift-users at swift.org> wrote:
> 
> // This little Swift program compiles (and runs) fine:
> 
> func foo() -> Int { return a }
> let a = 1
> let b = 2
> print(foo())
> 
> But if `foo()` returns `b` instead of `a`, I get this compile time error:
> "Use of unresolved identifier `b`”

Interesting.  Seems to have something to do with ordering as if you code it this way:

let a = 1
let b = 2
func foo() -> Int { return b }
print(foo())

it works fine (Xcode 8 playground).


More information about the swift-users mailing list