<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Feb 20, 2017, at 4:26 PM, Matthew Johnson &lt;<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a>&gt; wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 19, 2017, at 11:24 PM, John McCall &lt;<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 18, 2017, at 11:08 AM, Matthew Johnson &lt;<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a>&gt; wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi John,<div class=""><br class=""></div><div class="">This is fantastic! &nbsp;I’ve been eagerly anticipating this. &nbsp;It looks very much as I was hoping it would. &nbsp;I can’t wait to see this vision come to life!</div><div class=""><br class=""></div><div class="">You only show a mutating generator example. &nbsp;Do you also plan to allow shared generators?</div></div></div></blockquote><div class=""><br class=""></div>Yes; a generator yielding a shared reference would be used for non-mutating iteration.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">One topic you don’t touch on that feels related is the ability to use RAII and rely on deferred deinit to ensure an owned resource isn’t released before the scope exits. &nbsp;I can imagine something like `defer let resource = MyResource()`. &nbsp;It may also be interesting to support “defer only” types to force a compiler error where non-deferred use would be incorrect. &nbsp;What do you think?</div></div></div></blockquote><div class=""><br class=""></div>My intuition is the use cases for this that I'm aware of are really a mis-use of values. &nbsp;You might design an API that way in C++, where destructors are really the only language mechanism for injecting code into a function "later", but in Swift I think you would want to use either (1) a generalized accessor (if the protected resource could meaningfully be described as a single value) or (2) a callback that explicitly scopes what happens with the resource.</div><div class=""><br class=""></div><div class="">So e.g. if you wanted a Rust-style mutex API, you could do it like so:</div><div class=""><br class=""></div><div class="">moveonly struct Locked&lt;T&gt; {</div><div class="">&nbsp; var unlockedMemory: T { // probably not the best name</div><div class="">&nbsp; &nbsp; read {</div><div class="">&nbsp; &nbsp; &nbsp; mutex.acquire()</div><div class="">&nbsp; &nbsp; &nbsp; defer { mutex.release() } // There are reasons why I think this needs to be in a defer, but we can talk about them when we get to the detailed proposals for co-routines. &nbsp;I'm still looking for a better language design.</div><div class="">&nbsp; &nbsp; &nbsp; yield actualMemory // Recall that 'read' yields a shared value, so this doesn't implicitly copy.</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; nonmutating modify { // 'nonmutating' is a misnomer, but it's an existing misnomer. &nbsp;Setters are 'mutating' methods by default and normally demand exclusive access to self, but we don't need that here because we're dynamically enforcing exclusive access to the memory, so all we need is shared access, and this is how we express that.</div><div class="">&nbsp; &nbsp; &nbsp; mutex.acquire()</div><div class="">&nbsp; &nbsp; &nbsp; defer { mutex.release() }</div><div class="">&nbsp; &nbsp; &nbsp; yield &amp;actualMemory</div><div class="">&nbsp; &nbsp; }</div><div class="">&nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; private var mutex: Mutex</div><div class="">&nbsp; private mutable var actualMemory: T // Make this variable mutable even within nonmutating methods; whether that makes sense is the programmer's problem. &nbsp;I didn't cover this in the proposal, because it's speculative, but it's useful for things like our nonmutating modify. &nbsp;Lots of open questions here.</div><div class="">}</div></div></div></blockquote><div class=""><br class=""></div><div class="">I like this approach. &nbsp;It scopes access to the resource as necessary without requiring nesting.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Or if you wanted a more C-like mutex API, you'd do it like so:</div><div class=""><br class=""></div><div class="">moveonly struct Mutex {</div><div class="">&nbsp; func withLock&lt;T&gt;(_ function: () throws -&gt; T) rethrows -&gt; T {</div><div class="">&nbsp; &nbsp; acquire()</div><div class="">&nbsp; &nbsp; defer { release() }</div><div class="">&nbsp; &nbsp; return try function()</div><div class="">&nbsp; }</div><div class="">}</div><div class=""><br class=""></div><div class="">But I just don't see the motivation to try to do this by making a function return a C++-style lock guard.</div><div class=""><br class=""></div><div class="">Do you have a use case which clearly benefits from an exact scoping and really needs to be an independent value?</div></div></div></blockquote><div class=""><br class=""></div><div class="">To be honest, I don’t know. &nbsp;The primary thing I am looking for is a way to encapsulate the scoping of a resource without requiring the introduction of a new lexical scope like is required in your second example.</div></div></div></div></blockquote><div><br class=""></div>Yes, I agree that the nesting isn't ideal. &nbsp;In particular, it's awkward to get values out of the scope.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class="">The first approach you show above using generalized accessors may well be able to cover our needs. &nbsp;Thanks for taking time to show the example!</div></div></div></div></blockquote><div><br class=""></div>Okay, glad to hear it. &nbsp;If you *do* find something which really benefits from an RAII-style value, please let me know; I'm certainly open to the idea.</div><div><br class=""></div><div>John.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">John.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 17, 2017, at 11:08 AM, John McCall via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);" class=""><blockquote type="cite" class="" style="margin: 15px 0px;"><div class="" style="margin-top: 0px;">On Feb 17, 2017, at 4:50 AM, Adrian Zubarev &lt;<a href="mailto:adrian.zubarev@devandartist.com" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;">adrian.zubarev@devandartist.com</a>&gt; wrote:</div><div class="" style="margin-bottom: 0px;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="bloop_markdown"><p class="" style="margin: 15px 0px; -webkit-margin-before: 0px;">Hi John, would you mind creating a markdown document for this manifesto in<span class="Apple-converted-space">&nbsp;</span><a href="https://github.com/apple/swift/tree/master/docs" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none; -webkit-margin-before: 0px;">https://github.com/apple/swift/tree/master/docs</a>? :)</p><div class=""><br class="" style="-webkit-margin-before: 0px;"></div></div></div></div></blockquote>Yes, it should go in the repository. &nbsp;That commit is pending, but the in meantime, you can see the document properly rendered at:</div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);" class="">&nbsp;&nbsp;<a href="https://github.com/rjmccall/swift/blob/4c67c1d45b6f9649cc39bbb296d63663c1ef841f/docs/OwnershipManifesto.md" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;">https://github.com/rjmccall/swift/blob/4c67c1d45b6f9649cc39bbb296d63663c1ef841f/docs/OwnershipManifesto.md</a></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);" class=""><br class=""></div><div style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);" class=""><font face="Helvetica Neue, Helvetica, Arial, sans-serif" class="">John.</font></div><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254); float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);" class=""><span style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254); float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);" class=""><a href="mailto:swift-evolution@swift.org" style="color: rgb(65, 131, 196); background-color: rgb(254, 254, 254); text-decoration: none; font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="color: rgb(65, 131, 196); background-color: rgb(254, 254, 254); text-decoration: none; font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);" class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>