If the Swift 'guard' statement needs to go out of scope, what is the definition of scope?

I am confused about the definition of a block of code or "area". Apple security docs talk about it: else block security instructions ...

"must transfer control to exit the code block in which the security instruction appears."

Other online sources say that the β€œscope” in which it exists must be present in the security instruction.

So take the sample 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, the guard would simply have avoided this area. But this is not the case. Guard in this case leaves the function in which it is placed, regardless of whether it is similar in the if clause.

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

+7
scope swift
source share
3 answers

It is completely possible to do what you imagine, it’s just not what this particular code does. return always exits the method, not the local scope. To do what you want, 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 to the vanadian answer :

guard forces you out of scope with the transfer statement. 4 available:

  • return and throw both exits from the function / method
  • continue can be used in loops ( while / for / repeat-while )
  • break can be used in loops ( while / for / repeat-while ) to exit the immediate area. Specifying a label for a break allows you to immediately exit several areas (for example, tear out the structure of a nested loop). When using labels, break can also be used in the if .

Alternatively, you can go out of scope by calling a function that returns Never , such as fatalError .

+15
source share

Your understanding of the term "scope" is absolutely correct. Basically this is the space between two (balanced) brackets.

The description for exiting the code block in the documentation is the most accurate.

To exit the guard statement, you can use the return , break , continue or throw control transfer return .

  • return and throw terminates the entire function or method.
  • continue and break leaves the current scope (for example, switch , for or while ).
+2
source share

You misinterpret what the dock says. The security operator himself does not leave any area. You must write some statement inside the guard statement that goes out of the scope of the guard statement.

return will go out of scope. In a break or continue loop, they exit the scope. In many cases, goto will exit the stop. There is no special operator to exit the scope of the if statement, so you need a goto statement or something that leaves the loop or function containing the guard statement.

0
source share

All Articles