<div dir="ltr">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"><br><div class="gmail_quote">On Tue, Dec 29, 2015 at 11:15 PM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@apple.com" target="_blank">clattner@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Dec 29, 2015, at 8:02 PM, Trent Nadeau via swift-evolution <<a href="mailto:swift-evolution@swift.org">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 class="">> ```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>
… 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 class="HOEnZb"><font color="#888888"><br>
-Chris<br>
<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Trent Nadeau</div>
</div>