Defer Question


(Ezekiel Elin) #1

Is there any way for me to identify what value is/will be returned by the function in a defer block

here’s some example code

func someFunction() -> Int {
    defer {
        //Can I get the "5" that I'm returning here?
    }
    return 5
}

- Ezekiel


(Joe Groff) #2

Not implicitly. You could do something like this:

var returnValue: Int?
defer { use(returnValue!) }

...
returnValue = 5
return returnValue!

though that's obviously not ideal.

-Joe

···

On Dec 7, 2015, at 10:29 AM, Ezekiel Elin via swift-users <swift-users@swift.org> wrote:

Is there any way for me to identify what value is/will be returned by the function in a defer block

here’s some example code

func someFunction() -> Int {
    defer {
        //Can I get the "5" that I'm returning here?
    }
    return 5
}


(Slava Pestov) #3

Is there any way for me to identify what value is/will be returned by the function in a defer block

here’s some example code

func someFunction() -> Int {
    defer {
        //Can I get the "5" that I'm returning here?
    }
    return 5
}

Not implicitly. You could do something like this:

var returnValue: Int?
defer { use(returnValue!) }

...
returnValue = 5
return returnValue!

though that's obviously not ideal.

And of course if you entered the defer because you're throwing an error, 'returnValue' might not be set.

Slava

···

On Dec 7, 2015, at 10:38 AM, Joe Groff via swift-users <swift-users@swift.org> wrote:

On Dec 7, 2015, at 10:29 AM, Ezekiel Elin via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

-Joe

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users