<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="">This seems to just raise more questions than it answers. :p<div class=""><br class=""></div><div class="">Why would one be allowed but not the other? They should be equivalent.</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 24, 2016, at 5:49 AM, Zhao Xin &lt;<a href="mailto:owenzx@gmail.com" class="">owenzx@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">Below code works.</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif"><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="color:rgb(187,44,162)" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> MyValueType {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> x: () -&gt; () {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { </span><span style="color:rgb(61,29,129)" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="color:rgb(187,44,162)" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; }</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">() {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">//&nbsp; &nbsp; &nbsp; &nbsp; self.x = {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(self)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">//&nbsp; &nbsp; &nbsp; &nbsp; }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(79, 129, 135);" class=""><span style="color:rgb(187,44,162)" class="">let</span><span style="" class=""> a = </span><span style="font-variant-ligatures: no-common-ligatures;" class="">MyValueType</span><span style="" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(0, 132, 0);" class=""><span style="color:rgb(79,129,135)" class="">a</span><span style="" class="">.</span><span style="color:rgb(79,129,135)" class="">x</span><span style="" class="">() </span><span style="font-variant-ligatures: no-common-ligatures;" class="">// MyValueType()</span></div></div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sun, Jul 24, 2016 at 5:11 PM, Tyler Fleming Cloutier via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">Also if capture lists for structs do capture by value, how can I avoid the newly added error “Closure cannot implicitly capture a mutating self parameter” without changing the original reference semantics of MyValueType?</div><div class=""><br class=""></div><div class="">Also why can’t I specify weak self for value types in capture lists if a captured value type is really just a reference anyway? What’s the purpose of that?</div><div class=""><div class="h5"><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jul 24, 2016, at 2:07 AM, Tyler Fleming Cloutier &lt;<a href="mailto:cloutiertyler@aol.com" target="_blank" class="">cloutiertyler@aol.com</a>&gt; wrote:</div><br class=""><div class=""><div style="word-wrap:break-word" class="">The Swift Programming Language book states:<div class=""><br class=""></div><div class="">"<span style="color:rgb(65,65,65);font-family:Helvetica,Arial,sans-serif;background-color:rgb(249,249,249)" class="">Reference counting only applies to instances of classes. Structures and enumerations are value types, not reference types, and are not stored and passed by reference.</span><font color="#414141" face="Helvetica, Arial, sans-serif" class="">”</font></div><div class=""><br class=""></div><div class="">However consider the following example.</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">func</span><span class=""> makeIncrementer() -&gt; (() -&gt; </span><span style="color:#703daa" class="">Int</span><span class="">, () -&gt; </span><span style="color:#703daa" class="">Int</span><span class="">) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> runningTotal = </span><span style="color:#272ad8" class="">0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">func</span><span class=""> incrementer() -&gt; </span><span style="color:#703daa" class="">Int</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; runningTotal += </span><span style="color:#272ad8" class="">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#ba2da2" class="">return</span><span class=""> runningTotal</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">func</span><span class=""> incrementer2() -&gt; </span><span style="color:#703daa" class="">Int</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; runningTotal += </span><span style="color:#272ad8" class="">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#ba2da2" class="">return</span><span class=""> runningTotal</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">return</span><span class=""> (</span><span class="">incrementer</span><span class="">, </span><span class="">incrementer2</span><span class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)" class=""><span style="color:#ba2da2" class="">let</span><span class=""> x = </span><span class="">makeIncrementer</span><span class="">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="color:#4f8187" class="">x</span><span class="">.</span><span style="color:#272ad8" class="">0</span><span class="">() </span><span class="">// 1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#4f8187" class="">x</span><span class="">.</span><span style="color:#272ad8" class="">1</span><span class="">() </span><span style="color:#008400" class="">// 2</span></div></div><div class=""><br class=""></div><div class="">Clearly runningTotal is captured by reference by both incrementer and incrementer2. Is closure capturing the only case in which value types are treated as reference types?</div><div class=""><br class=""></div><div class="">Why wouldn’t Swift be implement so that runningTotal was Captured as a static copy at the time of capture so that each of the incrementer functions has their own “value”?</div><div class=""><br class=""></div><div class="">It seems like you could pretty easily run into a retain cycle with a struct even though it is a value type!</div><div class=""><br class=""></div><div class="">Consider:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="color:#ba2da2" class="">struct</span><span class=""> MyValueType {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> x: () -&gt; ()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">init</span><span class="">() {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#ba2da2" class="">self</span><span class="">.</span><span style="color:#4f8187" class="">x</span><span class=""> = {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#3e1e81" class="">print</span><span class="">(</span><span style="color:#ba2da2" class="">self</span><span class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span class=""><br class=""></span></div><div style="margin:0px;line-height:normal" class=""><span class=""><div style="font-family:Helvetica;font-size:12px" class="">One might imagine that the capture of a value type like MyValueType would make a static copy of self for the closure, but if I understand correctly I actually just get a retain cycle. But it’s even worse, because (as I understand it) whether I do or not is optimization dependent!</div><div style="font-family:Helvetica;font-size:12px" class=""><br class=""></div><div class="">"<span style="font-family:Helvetica,Arial,sans-serif;font-size:12px;color:rgb(65,65,65);background-color:rgb(249,249,249)" class="">As an optimization, Swift may instead capture and store a&nbsp;</span><em style="font-family:Helvetica,Arial,sans-serif;font-size:12px;border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(65,65,65)" class="">copy</em><span style="font-family:Helvetica,Arial,sans-serif;font-size:12px;color:rgb(65,65,65);background-color:rgb(249,249,249)" class="">&nbsp;of a value if that value is not mutated by a closure, and if the value is not mutated after the closure is created.</span><font color="#414141" face="Helvetica, Arial, sans-serif" class="">”</font></div><div style="font-family:Helvetica;font-size:12px" class=""><span style="color:rgb(65,65,65);font-family:Helvetica,Arial,sans-serif;background-color:rgb(249,249,249)" class=""><br class=""></span></div><div style="font-family:Helvetica;font-size:12px" class="">Now it turns out that as of SE-0035 I get an error for the above code. Namely,</div><div style="font-family:Helvetica;font-size:12px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px" class="">“Closure cannot implicitly capture a mutating self parameter.”</div><div style="font-family:Helvetica;font-size:12px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px" class="">Kind of a strange error message for two reasons. The first is that nothing appears to be doing any mutating or declaring an intention of mutating. The second is that it’s not clear at all (to me at least) how to go about remedying the issue.</div><div style="font-family:Helvetica;font-size:12px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px" class="">If I capture self explicitly with something like [s = self], am I getting a copy of self or a reference to self? I would assume a copy of self, but that’s not super clear given the somewhat surprising behavior of capturing value types by reference.</div><div style="font-family:Helvetica;font-size:12px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px" class="">I would think the following would be a valid solution. Is there a better one?&nbsp;</div><div style="font-family:Helvetica;font-size:12px" class=""><br class=""></div><div class=""><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span style="color:#ba2da2" class="">struct</span><span class=""> MyValueType {</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">var</span><span class=""> x: (() -&gt; ())!</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#ba2da2" class="">init</span><span class="">() {</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#ba2da2" class="">self</span><span class="">.</span><span style="color:#4f8187" class="">x</span><span class=""> = </span><span style="color:#ba2da2" class="">nil</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#ba2da2" class="">self</span><span class="">.</span><span style="color:#4f8187" class="">x</span><span class=""> = { [s = </span><span style="color:#ba2da2" class="">self</span><span class="">] </span><span style="color:#ba2da2" class="">in</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#3e1e81" class="">print</span><span class="">(s)</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class="">&nbsp; &nbsp; }</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal;min-height:13px" class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class=""></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class="">}</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal" class=""><span class=""><br class=""></span></div><div style="margin:0px;line-height:normal" class="">Is there a way to use currying to be more explicit about the semantics of passing a value into a closure?</div></div><div style="margin:0px;line-height:normal" class=""><br class=""></div><div style="margin:0px;line-height:normal" class="">Any help or discussion would be much appreciated!</div><div style="margin:0px;line-height:normal" class=""><br class=""></div><div style="margin:0px;line-height:normal" class="">Tyler</div></span></div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></blockquote></div><br class=""></div></div></div><br class="">_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>