[swift-evolution] Better syntax for deferred?

Robert S Mozayeni dev at mzy.me
Sat Jan 2 16:59:34 CST 2016

(Sorry if you get this email twice, I realized I first sent it from an email address that is not the one I used to subscribe to this list)

Not only that, but placing defer at the end of a scope, where any other code may never get executed if there’s an early return, kind of violates the whole concept of control flow.

func f() throws {
    let o = file(path: "")
    do {
        try o.write(self.data)

    always { o.close() }

What happens if o.write fails? Going with always would imply that we either…

A) put the `always` in every possible place the scope might exit, defeating the whole purpose of defer/always. Maury, I’m assuming you’re not actually suggesting that, which would leave: B) if the main scope of the function exits at any point, drop down to the `always` at the end of the scope and execute it. But then, what about the surrounding code at the end of the main scope? Like I said, I think this would violate the whole concept of control flow by cherry-picking a specific type of command that is always executed within a scope, even if that command is in some place the control flow doesn’t reach.

Unless I’m misinterpreting something (let me know if I am) this seems less intuitive than `defer` was to begin with.


> On Jan 2, 2016, at 3:17 PM, Dennis Lysenko via swift-evolution <swift-evolution at swift.org> wrote:
> Deferring at the end of the function removes the ability to defer actions on variables introduced in an inner scope.
> On Sat, Jan 2, 2016, 1:57 PM Tino Heth via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> I have the terrible feeling something is wrong with my posts so that they get caught by spamfilters or similar…
> But as others stated as well:
> defer has a use case that is a little bit different from what you want to archive.
> > Why not use a solution that is widely used and better?
> I'm curious:
> Which languages have this "always" construct?
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>  _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

More information about the swift-evolution mailing list