<div dir="ltr">[owned self] is weird. [self] is probably better and is currently the way to explicitly capture a variable.</div><br><div class="gmail_quote"><div dir="ltr">On Sun, Mar 5, 2017 at 6:35 AM Daniel Leping via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_msg">I think we can stretch it even further. Have an idea in mind.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Automatically propagated self is actually a big issue. Most of the newbies do A LOT of mistakes with it. So I thought: what if we restrict it even further? Like have no access to self *unless* it's explicitly passed. It can be done the very same way we do with [weak self] or [unowned self]? What if we introduce [owned self] and remove automatic self propagation?</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">This way one will have to at least think twice which propagation to choose instead of default strong reference which is not that good in many cases. Most for me, actually.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">If this idea gets any positive feedback and the issue is a headache not to me only I'll create a separate thread and/or proposal.</div><div class="gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg"><div class="gmail_msg">On Sat, 4 Mar 2017 at 21:55 Kenny Leung via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg"></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">Is <span style="background-color:rgb(255,255,255)" class="gmail_msg"><font face="Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">callback an </span><span style="font-size:14.666666984558105px" class="gmail_msg">autoclosure</span><span style="font-size:11pt" class="gmail_msg">, or just a regular argument?</span></font></span></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><span style="background-color:rgb(255,255,255)" class="gmail_msg"><font face="Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><br class="gmail_msg"></span></font></span></div><div class="gmail_msg"><span style="background-color:rgb(255,255,255)" class="gmail_msg"><font face="Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">-Kenny</span></font></span></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><span style="background-color:rgb(255,255,255)" class="gmail_msg"><font face="Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><br class="gmail_msg"></span></font></span></div><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Mar 3, 2017, at 1:14 PM, Alex Johnson via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="m_4562887335378662759m_9081232593702543186Apple-interchange-newline gmail_msg"><div class="gmail_msg"><div class="m_4562887335378662759m_9081232593702543186WordSection1 gmail_msg" style="font-family:Helvetica;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;background-color:rgb(255,255,255)"><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">Hi list members,<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">During code review today, I noticed a really subtle memory leak that looked like:<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"> self.relatedObject = RelatedObject(callback: relatedObjectDidFinish)<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">Where `relatedObject` is a strong reference, `callback` is an escaping closure, and `relatedObjectDidFinish` is a method of `self`. From a memory management perspective, this code is equivalent to:<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"> self.relatedObject = RelatedObject(callback: { self.relatedObjectDidFinish })<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">In the second example, an explicit `self.` is required. It’s my understanding that this is to highlight that the closure keeps a strong reference to `self`. But, when passing a method, there is no such requirement, which makes it easier to accidentally create a retain cycle.<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">This made me wonder if an explicit `self.` should be required when passing a method as an escaping closure. And whether that would help in the same way that the explicit `self.` *inside* the closure is intended to.<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">If it were required, the code in the first example would be:<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"> self.relatedObject = RelatedObject(callback: self.relatedObjectDidFinish)<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">What do you think?<u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><u class="gmail_msg"></u> <u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><b class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">Alex Johnson<u class="gmail_msg"></u><u class="gmail_msg"></u></span></b></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg"><a href="mailto:ajohnson@walmartlabs.com" style="color:rgb(149,79,114);text-decoration:underline" class="gmail_msg" target="_blank"><span style="color:rgb(5,99,193)" class="gmail_msg">ajohnson@walmartlabs.com</span></a><u class="gmail_msg"></u><u class="gmail_msg"></u></span></div><div style="margin:0in 0in 0.0001pt;font-size:12pt;font-family:Calibri" class="gmail_msg"><span style="font-size:11pt" class="gmail_msg">ajohnson on Slack</span><u class="gmail_msg"></u><u class="gmail_msg"></u></div></div><span style="font-family:Helvetica;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;background-color:rgb(255,255,255);float:none;display:inline!important" class="gmail_msg">_______________________________________________</span><br style="font-family:Helvetica;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;background-color:rgb(255,255,255)" class="gmail_msg"><span style="font-family:Helvetica;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;background-color:rgb(255,255,255);float:none;display:inline!important" class="gmail_msg">swift-evolution mailing list</span><br style="font-family:Helvetica;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;background-color:rgb(255,255,255)" class="gmail_msg"><a href="mailto:swift-evolution@swift.org" style="color:rgb(149,79,114);text-decoration:underline;font-family:Helvetica;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;background-color:rgb(255,255,255)" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica;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;background-color:rgb(255,255,255)" class="gmail_msg"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="color:rgb(149,79,114);text-decoration:underline;font-family:Helvetica;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;background-color:rgb(255,255,255)" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br style="font-family:Helvetica;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;background-color:rgb(255,255,255)" class="gmail_msg"></div></blockquote></div><br class="gmail_msg"></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div></div>
_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>