<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">If weak closures ever become a thing, I do think that the implicit strong promotion of the closure's captures should never happen and instead, the closure should be manually optional-unwrapped before it's called, so that the existing safety guarantees of weak object references would persist.<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 10, 2017, at 11:04 PM, Adrian Zubarev <<a href="mailto:adrian.zubarev@devandartist.com" class="">adrian.zubarev@devandartist.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><p style="margin: 15px 0px; -webkit-margin-before: 0px;" class="">Just a few more replies from John McCall:</p><blockquote style="margin: 15px 0px; border-left-width: 4px; border-left-style: solid; border-left-color: rgb(221, 221, 221); padding: 0px 15px; color: rgb(119, 119, 119);" class=""><p style="margin: 0px 0px 15px; -webkit-margin-before: 0px;" class="">You want to invalidate the closure value based on whether any of its captures have been invalidated.</p><p style="margin: 15px 0px;" class="">It’s an interesting idea, but it’s quite complex because an invalidatable closure is definitely a different sort of thing from a normal one.</p><p style="margin: 15px 0px;" class="">So like you say, it would need to be tracked in the type, which is not the language model we otherwise use for closures. Er, for weak/unowned, I mean.</p><p style="margin: 15px 0px;" class="">The behavior for unowned is not too different from the current behavior, so it’s really mostly about weak.</p><p style="margin: 15px 0px;" class="">I guess the captured reference becomes strong immediately, protecting against it going away during the closure.</p><p style="margin: 15px 0px;" class="">But unowned is conventionally used in cases where that isn’t going to happen anyway.</p><p style="margin: 15px 0px;" class="">The semantics of testing a “weak closure” are not obvious. Always must be wrapped in optional, unwrapping it gives a value of non-weak type?</p><p style="margin: 15px 0px 0px;" class="">Totally different from normal optional behavior on values, but I guess it works.</p></blockquote><div style="margin: 15px 0px;" class=""><br class="webkit-block-placeholder"></div></div><div class="bloop_original_html" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class=""><br class=""></div><br class=""><div id="bloop_sign_1497124891522742016" class="bloop_sign"><div style="font-family: helvetica, arial; font-size: 13px;" class="">-- <br class="">Adrian Zubarev<br class="">Sent with Airmail</div></div><br class=""><p class="airmail_on" style="margin: 15px 0px;">Am 10. Juni 2017 um 21:58:21, Adrian Zubarev (<a href="mailto:adrian.zubarev@devandartist.com" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;" class="">adrian.zubarev@devandartist.com</a>) schrieb:</p><blockquote type="cite" class="clean_bq" style="margin: 15px 0px;"><span style="margin-top: 0px; margin-bottom: 0px;" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""></div><div class=""><div class="bloop_markdown"><p style="margin: 15px 0px; -webkit-margin-before: 0px;" class="">Well I simply reused the existing keywords as part of the closure type to demonstrate the idea here as easy as possible.</p><p style="margin: 15px 0px;" class="">About the _true_ weak/unowned closures. First I asked John McCall on twitter about this whole idea. Here is his reply about the _true_ weak closures:</p><blockquote style="margin: 15px 0px; border-left-width: 4px; border-left-style: solid; border-left-color: rgb(221, 221, 221); padding: 0px 15px; color: rgb(119, 119, 119);" class=""><div style="margin: 0px; -webkit-margin-before: 0px;" class="">You mean a weak reference to a closure? No, I don’t think we’d ever want to assign closure values enough identity to give that meaning.</div></blockquote><p style="margin: 15px 0px;" class="">I get it, that it might be not quite obvious or even confusing why<span class="Apple-converted-space"> </span><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;" class="">weak/unowned</code><span class="Apple-converted-space"> </span>can be applied on a type and on a variable. However<span class="Apple-converted-space"> </span><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;" class="">weak</code><span class="Apple-converted-space"> </span>is always an<span class="Apple-converted-space"> </span><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;" class="">Optional</code><span class="Apple-converted-space"> </span>or an<span class="Apple-converted-space"> </span><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;" class="">IUO</code><span class="Apple-converted-space"> </span>where<span class="Apple-converted-space"> </span><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;" class="">unowned</code><span class="Apple-converted-space"> </span>is non-optional.</p><p style="margin: 15px 0px;" class="">Just my personal thoughts: Could we potentially align<span class="Apple-converted-space"> </span><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;" class="">weak/unowned</code><span class="Apple-converted-space"> </span>with<span class="Apple-converted-space"> </span><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;" class="">inout</code><span class="Apple-converted-space"> </span>and make it type decoration? (This would be a breaking change.)</p></div><div class="bloop_original_html"><div id="bloop_customfont" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;" class=""><br class=""></div><br class=""><div id="bloop_sign_1497124159511079168" class="bloop_sign"><div style="font-family: helvetica, arial; font-size: 13px;" class="">-- <br class="">Adrian Zubarev<br class="">Sent with Airmail</div></div><br class=""><p class="airmail_on" style="margin: 15px 0px;">Am 10. Juni 2017 um 21:44:41, Gor Gyolchanyan (<a href="mailto:gor@gyolchanyan.com" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;" class="">gor@gyolchanyan.com</a>) schrieb:</p><blockquote type="cite" class="clean_bq" style="margin: 15px 0px;"><div class="" style="margin-top: 0px; margin-bottom: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class=""><span class="">I don't think it's a good idea to make the `weak`-ness of the closure part of the type system.</span><div class=""><span class="">The current approach to `weak/unowned` is that it's a storage class that applies to the variable, not the value.</span></div><div class=""><span class="">This makes sense because you don't *use* a weak reference, you only *store* it weakly.</span></div><div class=""><span class="">I think this behavior has to stay the same with the addition of closures (being the other reference type along classes) would be eligible for storing by `weak/unowned` reference.<br class=""></span><div class=""><span class=""><br class=""></span><blockquote type="cite" class="" style="margin: 15px 0px;"><div class="" style="margin-top: 0px;"><span class="">On Jun 10, 2017, at 10:39 PM, Adrian Zubarev via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;">swift-evolution@swift.org</a>> wrote:</span></div><span class=""><br class="Apple-interchange-newline"></span><div class="" style="margin-bottom: 0px;"><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><p class="" style="margin: 15px 0px; -webkit-margin-before: 0px;"><span style="-webkit-margin-before: 0px;" class="">Hi Matthew,</span></p><p class="" style="margin: 15px 0px;"><span style="-webkit-margin-before: 0px;" class="">Thank you for the hint. Indeed, at first glance your proposal is very similar to my thinking. I’ll try to dive deeper into your proposal and find out more about the discussion behind it when I get some more time for that.</span></p><p class="" style="margin: 15px 0px;"><span style="-webkit-margin-before: 0px;" class="">We could collaborate on a revision for Swift 5 if you would like to, but we wouldn’t need to rush now, because there is plenty of time for that.<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span></span></p><p class="" style="margin: 15px 0px;">Personally I would love to avoid some kind of a prefix like<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">?</code><span class="Apple-converted-space"> </span>if possible.</p><p class="" style="margin: 15px 0px;">Brainstorming:</p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">let c1 = weak someObject.method</code><span class="Apple-converted-space"> </span>convert it to a weak closure.</p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">let c2: weak (CLOSURETYPE)? = someObject.method</code><span class="Apple-converted-space"> </span>Let the compiler do the job for us.</p><p class="" style="margin: 15px 0px;">The latter would be great for APIs to avoid the prefix.</p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">.subscribe(onNext: self.bar)</code><span class="Apple-converted-space"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;">weak</code><span class="Apple-converted-space"> </span>is inferred and can be omitted<span class="Apple-converted-space"> </span></p><div class="" style="margin: 15px 0px;"><br class="webkit-block-placeholder" style="-webkit-margin-before: 0px;"></div></div><div class="bloop_original_html" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;"><br class=""></div><br class=""><div id="bloop_sign_1497122959888763904" class="bloop_sign"><div class="" style="font-family: helvetica, arial; font-size: 13px;">-- <br class="">Adrian Zubarev<br class="">Sent with Airmail</div></div><br class=""><p class="airmail_on" style="margin: 15px 0px;">Am 10. Juni 2017 um 21:09:28, Matthew Johnson (<a href="mailto:matthew@anandabits.com" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;">matthew@anandabits.com</a>) schrieb:</p><blockquote type="cite" class="clean_bq" style="margin: 15px 0px;"><div class="" style="margin-top: 0px; margin-bottom: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class=""></div><div class=""><span class="" style="margin-top: 0px; margin-bottom: 0px;">Hi Adrian, this is pretty similar to the Guarded Closures proposal I drafted in February. This proposal needs a revision incorporating discussion feedback and some new ideas. If you’re interested, here’s a link to the original discussion thread: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170213/032478.html" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170213/032478.html</a>.</span><div class=""><span class="" style="margin-top: 0px; margin-bottom: 0px;"><br class=""></span><div class=""><blockquote type="cite" class="" style="margin: 15px 0px;"><div class="" style="margin-top: 0px;"><span class="" style="margin-top: 0px; margin-bottom: 0px;">On Jun 10, 2017, at 12:29 PM, Adrian Zubarev via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;">swift-evolution@swift.org</a>> wrote:</span></div><span class="" style="margin-top: 0px; margin-bottom: 0px;"><br class="Apple-interchange-newline"></span><div class="" style="margin-bottom: 0px;"><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><p class="" style="margin: 15px 0px; -webkit-margin-before: 0px;"><span class="" style="-webkit-margin-before: 0px; margin-top: 0px; margin-bottom: 0px;">Hello Evolution,</span></p><p class="" style="margin: 15px 0px;"><span class="" style="-webkit-margin-before: 0px; margin-top: 0px; margin-bottom: 0px;">I’d like to pitch a new idea and see where it would go. Recently I tapped into a small trap and just now realized that even that<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">non-escaping</code><span class="Apple-converted-space"> </span>should have been the default for closures (SE–0103) there is an exception for that. Apparently generics don’t follow that rule and a closure like<span class="Apple-converted-space"> </span></span></p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">Optional<() -> Void></code><span class="Apple-converted-space"> </span>or simply<span class="Apple-converted-space"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;">(() -> Void)?</code></p><p class="" style="margin: 15px 0px;">is still escaping by default. But that was the half of the story yet. As we all know and “love” reference lists inside closures, methods don’t have any and we have to wrap method calls into a weak referenced closure<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span></p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">{ [weak self] in self.foo() }</code></p><p class="" style="margin: 15px 0px;">to avoid strong reference cycles. Maybe you already guess it, I accidentally didn’t and tapped into the land of strong reference cycles yet again on my journey.</p><p class="" style="margin: 15px 0px;">I’d like to pitch a new way, more like a new type behavior, for closures on how they could be used differently in order to avoid strong reference cycles but also providing the ability to use methods without any need to wrap them.</p><p class="" style="margin: 15px 0px;">Here is a simple code snippet using RxSwift, which will recreate my issue:</p><pre class="" style="margin: 15px 0px; font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(204, 204, 204); overflow: auto; padding: 4px 8px; word-break: normal; word-wrap: normal;"><code class="swift" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 0px; margin: 0px; padding: 0px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">import RxSwift
let test = PublishSubject<Void>()
class A {
let disposeBag = DisposeBag()
func foo() {
test.asObservable()
.subscribe(onNext: self.bar) // The issue is here
.disposed(by: self.disposeBag)
}
func bar() { print("works") }
}
let a = A()
a.foo()
test.onNext(()) // Testing if it works
test.onCompleted() // Some RxSwift stuff
</code></pre><p class="" style="margin: 15px 0px;">In this case by passing directly the method<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">self.bar</code><span class="Apple-converted-space"> </span>we’re capturing<span class="Apple-converted-space"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;">self</code>, which in this situation isn’t our intention at all. To avoid this issue we can simply wrap the method call into closure:</p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">.subscribe(onNext: { [unowned self] in self.bar() })</code></p><p class="" style="margin: 15px 0px;">(It’s safe to make it unowned because the dispose bag is a member of<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">self</code>.)</p><hr class="" style="height: 0.2em; border: 0px; color: rgb(204, 204, 204); background-color: rgb(204, 204, 204); display: inherit;"><p class="" style="margin: 15px 0px;">What if we had the ability for weak or unowned closures? By that I don’t mean weak/unowned references to the closures themselves, because they are also reference types, but an invalidation behavior for the whole closure based on the _captured_ references. For instance:</p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">let closure1: weak (() -> Void)? = { self.doWhatever() }</code></p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">let closure2: weak (() -> Void)? = self.doWhatever</code></p><p class="" style="margin: 15px 0px;">If one would now try to call the<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">closure</code>, first it will check if all the captured objects are still available or not, if not the whole closure in this case will simply become<span class="Apple-converted-space"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;">nil</code><span class="Apple-converted-space"> </span>and won’t execute. In case of unowned closures it will trap. Furthermore it will support the general meaning of<span class="Apple-converted-space"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal;">weak/unowned</code><span class="Apple-converted-space"> </span>and will not increase the reference counter for *captured objects*.</p><p class="" style="margin: 15px 0px;">As you have already noticed, in this case the convention is slightly different because we must carry the behavior directly with the type.</p><p class="" style="margin: 15px 0px;"><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">func subscribe(onNext: weak ((Swift.E) -> Void)?)</code></p><p class="" style="margin: 15px 0px;">If the way of my thinking is correct this idea _could maybe_ fade out the very common<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">[weak self] in guard let strongSelf = self …</code><span class="Apple-converted-space"> </span>pattern.<span class="Apple-converted-space"> </span></p><hr class="" style="height: 0.2em; border: 0px; color: rgb(204, 204, 204); background-color: rgb(204, 204, 204); display: inherit;"><p class="" style="margin: 15px 0px;">I personally cannot tell all the technical difficulties this idea might have, but that’s what the evolution list is for, to collaboratively flesh out the ideas if they are worth it.</p><hr class="" style="height: 0.2em; border: 0px; color: rgb(204, 204, 204); background-color: rgb(204, 204, 204); display: inherit;"><p class="" style="margin: 15px 0px;">If something like this could be possible it’s probably worth noting that we might also be able to introduce something like<span class="Apple-converted-space" style="-webkit-margin-before: 0px;"> </span><code class="" style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;">@autoclosure(weak/unowned)</code><span class="Apple-converted-space"> </span>to Swift for consistency.</p><div class="" style="margin: 15px 0px;"><br class="webkit-block-placeholder" style="-webkit-margin-before: 0px;"></div></div><div class="bloop_original_html" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><div id="bloop_customfont" class="" style="font-family: Helvetica, Arial; font-size: 13px; margin: 0px;"><br class=""></div><br class=""><div id="bloop_sign_1497112926484879104" class="bloop_sign"><div class="" style="font-family: helvetica, arial; font-size: 13px;">-- <br class="">Adrian Zubarev<br class="">Sent with Airmail</div></div></div><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><div class="" style="-webkit-margin-before: 0px; margin: 15px 0px;"><br class="webkit-block-placeholder" style="-webkit-margin-before: 0px;"></div></div><span class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254); float: none; display: inline !important;">_______________________________________________</span><br class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><span class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254); float: none; display: inline !important;">swift-evolution mailing list</span><br class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><a href="mailto:swift-evolution@swift.org" class="" 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; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">swift-evolution@swift.org</a><br class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="" 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; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"></div></blockquote></div><br class=""></div></div></div></blockquote></div><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><div class="" style="-webkit-margin-before: 0px; margin: 15px 0px;"><br class="webkit-block-placeholder" style="-webkit-margin-before: 0px;"></div></div><span class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254); float: none; display: inline !important;">_______________________________________________</span><br class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><span class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254); float: none; display: inline !important;">swift-evolution mailing list</span><br class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><span class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254); float: none; display: inline !important;"><a href="mailto:swift-evolution@swift.org" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;">swift-evolution@swift.org</a></span><br class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><span class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254); float: none; display: inline !important;"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="" style="color: rgb(65, 131, 196); background-color: inherit; text-decoration: none;">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class="" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"></div></blockquote></div><br class=""></div></div></div></blockquote></div><div class="bloop_markdown"></div></div></div></span></blockquote></div><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><br class="Apple-interchange-newline"></div></div></blockquote></div><br class=""></body></html>