<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-family:helvetica;font-size:12px">lifetime guaranteed to be around until the method call completes</span></blockquote><div><br></div><div>AFAIK, this is true.</div><div>However, in this case, it&#39;s not a method call. The lifetime is only guaranteed until <font face="monospace, monospace">getter:increment</font> completes.</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(79,129,135);font-variant-ligatures:no-common-ligatures">Demo</span><span style="font-variant-ligatures:no-common-ligatures">().</span><span style="color:rgb(79,129,135);font-variant-ligatures:no-common-ligatures">increment</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">3</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">|--------------| lifetime</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><br></div><div style="font-size:14px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"></span></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-02-22 3:00 GMT+09:00 Ray Fix via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi,</div><div><br></div><div>The following code crashes:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">class</span><span style="font-variant-ligatures:no-common-ligatures"> Demo {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">var</span><span style="font-variant-ligatures:no-common-ligatures"> value = </span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">lazy</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">var</span><span style="font-variant-ligatures:no-common-ligatures"> increment: (</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Void</span><span style="font-variant-ligatures:no-common-ligatures"> = { [unowned </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">self</span><span style="font-variant-ligatures:no-common-ligatures">] by </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">self</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">value</span><span style="font-variant-ligatures:no-common-ligatures"> += by</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">self</span><span style="font-variant-ligatures:no-common-ligatures"><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">value</span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(79,129,135);font-variant-ligatures:no-common-ligatures">Demo</span><span style="font-variant-ligatures:no-common-ligatures">().</span><span style="color:rgb(79,129,135);font-variant-ligatures:no-common-ligatures">increment</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">3</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="Menlo"><span style="font-size:11px">error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0).</span></font></div><div style="margin:0px;line-height:normal"><br></div></span></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="font-family:Helvetica;font-size:12px">However if I call it this way:</div><div style="font-family:Helvetica;font-size:12px"><br></div><div style="font-family:Helvetica;font-size:12px"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">do</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> demo = </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">Demo</span><span style="font-variant-ligatures:no-common-ligatures">()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">  demo.</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">increment</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#272ad8">3</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></div></div><div><div style="font-family:Helvetica;font-size:12px">All is well.  This breaks my mental model of lifetime guaranteed to be around until the method call completes.</div></div><div style="font-family:Helvetica;font-size:12px">Is it me that is wrong or the playground.  Is the second way working just by luck?</div></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>