[swift-users] Can anyone please explain this behavior?
Jens Persson
jens at bitcycle.com
Wed Sep 21 18:29:16 CDT 2016
I asked the same question on twitter, and it resulted in some interesting
finds:
https://twitter.com/bitCycle/status/778697998893142016
On Thu, Sep 22, 2016 at 12:41 AM, Zhao Xin <owenzx at gmail.com> wrote:
> I suggest you defining the variables before using them like Marco does.
> Although from the global variable point of view, both way should be fine.
> But the glitch does exist, especially in playground.
>
> Zhaoxin
>
> On Thu, Sep 22, 2016 at 5:59 AM, Marco S Hyman via swift-users <
> swift-users at swift.org> wrote:
>
>>
>> > 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).
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160922/3bd3d3c6/attachment.html>
More information about the swift-users
mailing list