Hi Swift Evolution Community,
Thanks for the forum in place I have been reading the mailing list since the start but never felt comfortable enough to contribute so please bear with me it's my first pitch.
What if defer
can have the return value of a function as a parameter.
// today defer
func toto() -> Int {
let lock = Lock() defer { lock.unlock }
return 42
}
// tomorrow defer
func toto() -> Int {
let lock = Lock() defer { a in
lock.unlock
sendToStatBackend(a)
}
return 42
}
// tomorrow defer with source compat fix
func toto() -> Int {
let lock = Lock() defer { $0; lock.unlock } // ugly but no source breakage since it add at compile time
return 42
}
It can work without breaking the source compatibility if we add $0
in today defer at compile time.
Swift 4.x - Adding a $0;
at the beginning of the closure a compile time to dodge the warning "Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored"
Swift 5.0 - Not adding automatically the $0;
to restore the warning "Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored"
What do you think about it?