func someone() -> String {
defer {
return "World"
}
return "Hello"
}
someone()
for above snippet of code its throwing error "'return' cannot transfer control out of a defer statement" so what is the reason behind it?
Returning more than one value is not allowed, regardless of whether you try to do that in a defer
statement or not.
Whatever you do to mutate a return value in a defer statement, that's not accessible via a return. It happens afterward.
func someone() -> String {
var `return` = "Hello"
defer {
`return` += "World"
}
return `return`
}
someone() // Hello