This is what I thought about with return labels, a function's return label is the function name and a variables return label is the variable name. This mirrors how labels already work with control statements.
func foo() -> Int {
var a: Int = if bar {
// label explicit for example,
// where `return 1` would return 1 to `a`
return a: 1
} else {
// reduced example
return foo: 2
}
// do stuff with `a`
}
This would actually work just but only the first trailing closure cannot have a label, but this seems unpopular. So I could see the introduction of that behavior/syntax leading to first trailing closure labels - therefore filling in another perceived gap of the language and ushering in the return of a desired feature.
// current
foo(...) {
// n/a
} bar: { // assume type: () -> Int
let a: Int = if {
// label explicit for example
return a: 1
} else {
// reduced example
return bar: 2
}
// do stuff with `a`
}
// future
foo(...) bar: { // assume type: () -> Int
let a: Int = if {
// label explicit for example
return a: 1
} else {
// reduced example
return bar: 2
}
// do stuff with `a`
} { ... } // assume I have any number of trailing closures
Edit: another example after my morning coffee
// bar is the label for the previously anonymous closure
let foo: () -> Int = bar: {
let a: Int = if {
return a: 1
} else {
return bar: 2
}
}()