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 exit 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?
You are misinterpreting what the doc says. The guard statement itself doesn't exit any scope. YOU must write some statement inside the guard statement that exits the scope containing the guard statement.
return will exit the scope. In a loop, break or continue will exit the scope. In many cases, goto will exit the stop. There is no specific statement to exit the scope of an if statement, so you would need a goto statement or something that exits the loop or the function containing the guard statement.
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, andbreak
: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
andthrow
both exit the function/methodcontinue
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 inif
scopes.Additionally, you may exit the scope by calling a function that returns
Never
, such asfatalError
.Your understanding of the term scope is absolutely correct. Basically it's the space between two (balanced) braces.
The description to exit the code block in the documentation is the most accurate.
To exit a
guard
statement you can use the control transfer statementsreturn
,break
,continue
, orthrow
.return
andthrow
exits the whole function or method.continue
andbreak
exits the current scope (for exampleswitch
,for
orwhile
).