[swift-users] Redeclaration of guard variable is ignored at top-level

Martin R martinr448 at gmail.com
Fri Jun 17 00:18:53 CDT 2016


Hi,

I wonder why the Swift compiler does not complain about the
redeclaration of `number` after the guard-statement in top-level code:

    // main.swift
    import Swift

    guard let number = Int("1234") else { fatalError() }
    print(number) // Output: 1234
    let number = 5678
    print(number) // Output: 1234

It looks as if the statement `let number = 5678` is completely ignored.

However, doing the same inside a function causes a compiler error:

    func foo() {
        guard let number = Int("1234") else { fatalError() }
        print(number)
        let number = 5678 //  error: definition conflicts with previous value
    }

Tested with
- Xcode 7.3.1, "Default" and "Snapshot 2016-06-06 (a)" toolchain
- Xcode 8 beta.

Am I overlooking something or is that a bug?

Martin


More information about the swift-users mailing list