thecloud_of_unknowing thecloud_of_unknowing - 3 months ago 17
Swift Question

If the Swift 'guard' statement must exit scope, what is the definition of scope?

I am confused about the definition of a code block or of 'scope'. Apple docs for guard say this: the else block of a guard statement...


"must transfer control to exit the code block in which the guard statement appear."


Other online sources say the guard statement must exist the 'scope' in which it exists.

So take the example code below:

func testGuardControlFlow () {

let x = 2
let y = 2

func embededFunc () {

if y == 2 {

guard x == 1 else {
print("oops, number is not 1")
return
}

print ("from in embededFunc")

}

print ("I still want this to print even if x != 1")
}

embededFunc()
print("Great, return still allows this to be printed.")

}

testGuardControlFlow()


According to my current understanding of 'scope', the code

if y == 2 {....}


creates a new scope, namely between { }. And given this assumption, guard would merely escape that scope. But that is not the case. Guard in this instance escapes from the function it is placed in, irrespective of whether it is buried in an if clause.

Am I completely misunderstanding what 'scope' means? Does scope mean the code contained in a method? If so, what is the correct term for the 'space' that exists within an if statement?

Answer

It is totally possible to do what you envision, it just happens to not be what that particular code does. return always exits a method, not the local scope. To do what you wish, you can use a label, and break:

func testGuardControlFlow () {

    let x = 2
    let y = 2

    func embededFunc () {

        breakLabel:
        if y == 2 {

            guard x == 1 else {
                print("oops, number is not 1")
                break breakLabel
            }

            print ("from in embededFunc")

        }

        print ("I still want this to print even if x != 1")
    }

    embededFunc()
    print("Great, return still allows this to be printed.")

}

testGuardControlFlow()

To add on to vadian's answer:

guard forces you to exit the scope using a control transfer statement. There are 4 available to you:

  • return and throw both exit the function/method
  • continue can be used within loops (while/for/repeat-while)
  • break can be used in loops (while/for/repeat-while) to exit the immediate scope. Specifying a label to break to will allow you to exit multiple scopes at once (e.g. breaking out of nested loop structure). When using a label, break can also be used in if scopes.