Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defer keyword documentation is misleading a little. #292

Open
NMaks07 opened this issue Apr 17, 2024 · 0 comments
Open

defer keyword documentation is misleading a little. #292

NMaks07 opened this issue Apr 17, 2024 · 0 comments

Comments

@NMaks07
Copy link

NMaks07 commented Apr 17, 2024

Location

image

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´

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant