defer keyword documentation is misleading a little.
Opened this issue · 0 comments
NMaks07 commented
Location
Description
when I read ´outside of the scope that the defer statement appears in.´ I assume that scope is above and below of ´defer´ statement. But this is wrong. defer will work only at half of scope, which is below ´defer´ statement.
below code will execute ´defer´ right after of guard body's ´return´ statement
func someFunction() {
defer {
print("defer works")
}
guard false else {
print("inside guard")
return
}
print("bottom of function")
}
// prints
// inside guard
// defer works
but if we put ´defer´ after guard statement, then ´defer´will not be invoked:
func someFunction() {
guard false else {
print("inside guard")
return
}
defer {
print("defer works")
}
print("bottom of function")
}
// prints inside guard
Correction
Maybe you should document ´defer´ like as ~ ´right after defer statement appears in´