<div dir="ltr"><div class="gmail_extra">I disagree that `do using` would be useless for resources. Essentially no Cocoa resource classes have a `deinit`. NSFileHandle, NSStream, etc. require a close-like call to free the underlying resource. Also, per the Swift documentation about deinitialization:</div><div class="gmail_extra"><br></div><div class="gmail_extra"> "Typically you don’t need to perform manual clean-up when your instances are deallocated. However, when you are working with your own resources, you might need to perform some additional clean-up yourself. For example, if you create a custom class to open a file and write some data to it, you might need to close the file before the class instance is deallocated."<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 30, 2015 at 1:19 AM, Félix Cloutier <span dir="ltr"><<a href="mailto:felixcca@yahoo.ca" target="_blank">felixcca@yahoo.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>My understanding is that you suggest using the "do using" statement for two purposes:</div><div><br></div><div><ol><li>to deterministically free resources (files, sockets);</li><li>to create a scope with a guarantee about something (locks).</li></ol><div><br></div></div><div>The first purpose is very relevant for garbage-collected languages because the GC generally only monitors memory pressure to decide when to run, and so the GC won't budge if you're running out of file descriptors even if some could be reclaimed. However, Swift is not garbage-collected and resources are already reclaimed deterministically. If you create a Swift object that represents a file descriptor and don't allow references to escape the function, the object will be destroyed (and its resources reclaimed) at the latest when the function returns. In my opinion, this makes a "do using" statement useless for resource management.</div><div><br></div><div>For scopes with guarantees, as Chris said, the most common pattern is to have a function that accepts a closure. I've seen some pretty serious nesting with `if let` (which are one very frequent case of scopes with guarantees), but other than that, I don't see lots of nesting and I've been pretty happy with what the language can do so far. The only thing I can complain about is that you can't use break/continue with the current setup.</div><div><br></div><div>I see the discrepancy between objects, but I would say that any scope-based solution will often be just as hard to discover as the current solutions. `do using AutoreleasePool() { ... }` isn't an improvement over `autoreleasepool { ... }`.</div><div><br></div><div>It's better for the lock case, but only if you agree that `do using` is useless for resource management. Otherwise, if your mutex itself benefits from being scoped, `do using lock` is probably creating/deleting a mutex, and you'd need to use `do using Lock(mutex) { ... }` to actually lock it (like with C++ mutex/lock objects), which is as discoverable as `mutex.lock { ... }`.</div><span><font color="#888888"><br><div>
<span style="border-collapse:separate;line-height:normal;border-spacing:0px">Félix</span>
</div>
<br></font></span><div><blockquote type="cite"><div><div><div>Le 29 déc. 2015 à 23:24:53, Trent Nadeau via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> a écrit :</div><br></div></div><div><div><div><div dir="ltr" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">While useful, that pattern doesn't seem to compose well. What if you need two locks? Would that be:<br><br>lock1.withThisLockHeld {<div> lock2.withThisLockHeld {</div><div> // statements</div><div> }</div><div>}</div><div><br></div><div>If so, it seems like it has the "pyramid of doom" issue that prompted allowing `if let` to have multiple bindings.</div><div><br></div><div>In addition to the possible indentation and vertical space issue, you need to look up if and how each resource type does this. I believe this is a general enough pattern that it deserves language support. I think an analogy to the current situation would be if each collection type had its own way to iterate (Array.forEach, Set.withEachElement, etc.) instead of having for-in.</div></div><div class="gmail_extra" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br><div class="gmail_quote">On Tue, Dec 29, 2015 at 11:15 PM, Chris Lattner<span> </span><span dir="ltr"><<a href="mailto:clattner@apple.com" target="_blank">clattner@apple.com</a>></span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span>On Dec 29, 2015, at 8:02 PM, Trent Nadeau via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>> Doing this manually is possible using `defer` statements among other options, but this is error prone as a `defer` can be forgotten, `lock`/`unlock` calls for two locks can be switched due to a typo, etc. Having a dedicated language construct for this common case makes it easier to read and write while making code shorter and clearer.<br>><br></span><span>> ```swift<br>> do {<br>> lock.enterScope()<br>> defer { lock.exitScope() }<br>><br>> let file = try getFileHandle()<br>> file.enterScope()<br>> defer { file.exitScope() }<br>><br>> // statements<br>> }<br><br></span>We have another pattern that types can use, which is:<br><br>lock.withThisLockHeld {<br> <span> </span>… stuff ...<br>}<br><br>This can be done today with trailing closures. Other examples of this are “autoreleasepool” and withUnsafePointer (for other reasons).<br><span><font color="#888888"><br>-Chris<br><br></font></span></blockquote></div><br><br clear="all"><div><br></div>--<span> </span><br><div>Trent Nadeau</div></div></div></div><span><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k6QnlI05dewaN4Ks2UnXzaxygN-2FXAb6KzgD2KVzo9l6ee339lHBusRscaX70xQUb58MAgfs5qtmK-2FahukqU9Sjvrilfo85rdXGMPllD7u6k2CbxrL2myERJX3H8rXqyhd-2BRUws3ozQ4okGACfiQ8anqb-2BFtjXxutzaGiCLZ4c78meiBNcYWa-2BF0OidegYQ7FI4-3D" alt="" width="1" height="1" border="0" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important"><span> </span>_______________________________________________</span><br style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-evolution mailing list</span><br style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-evolution@swift.org" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span></div></blockquote></div><br></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div>Trent Nadeau</div>
</div></div>