[swift-evolution] Better syntax for deferred?

Charles Srstka cocoadev at charlessoft.com
Sat Jan 2 16:29:59 CST 2016

> On Jan 2, 2016, at 8:37 AM, Tino Heth via swift-evolution <swift-evolution at swift.org> wrote:
>> Unless I missed something obvious, wouldn't placing "code that always has to run at the end" actually *at the end* not make more sense? Like this…
> In most cases, you use defer for cleanup tasks - so it make more sense to keep it at the source of the "problem":
> file.open(); defer { file.close() }
> Tino

This. It’s way easier to remember to do necessary cleanup tasks if you add the cleanup call right after the call that requires the cleanup. It’s also much easier to catch cases where someone has forgotten to do so. Separating the init and cleanup by large distances as in the old try/catch/finally mechanism makes it easier for things to get out of sync as the code evolves.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160102/5dadd157/attachment.html>

More information about the swift-evolution mailing list