<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="">It seems to me you are shooting while blindfolded.<div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="gmail_default"><font face="georgia, serif" class="">I changed you code `</font><span class="" style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">let</span><span class="" style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">&nbsp;result = closure(n)`&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><font face="georgia, serif" class="">to</font></span><span class="" style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">&nbsp;`<span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;result = closure(</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);">1</span><span class="" style="font-variant-ligatures: no-common-ligatures;">)</span>`</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><font face="georgia, serif" class="">. I thought that should eliminate the error. It didn't. The compiler asked me to change the whole code as below:</font><br class=""></span></div></div></blockquote></div><div class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><font face="georgia, serif" class=""><br class=""></font></span></div><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><font face="georgia, serif" class="">Why did you expect changing which argument to send the closure to make any difference?</font></span></div></div></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">func</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;mainFunction(closure:&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);">@escaping</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;(</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Int</span><span class="" style="font-variant-ligatures: no-common-ligatures;">) -&gt;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Int</span><span class="" style="font-variant-ligatures: no-common-ligatures;">) -&gt;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Int</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;{</span></div></span></div></div></blockquote></div><div class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">Sure declaring the closure as escaping shuts up the compiler. At the cost of having an escaping closure in a context where the closure is guaranteed no to escape. My understanding of the distinction between escaping and non escaping is it enables compiler optimisations (in the non escaping case) that would not be possible in the general case. Therefore this compiler-mandated change kills performance for nothing</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><blockquote type="cite" style="font-family: Helvetica; font-size: 12px;" class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><font face="georgia, serif" class="">So I think in `</font></span><span class="" style="color: rgb(186, 45, 162); font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">func</span><span class="" style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">&nbsp;closureDoubled(</span><span class="" style="color: rgb(186, 45, 162); font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">_</span><span class="" style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">&nbsp;n:&nbsp;</span><span class="" style="color: rgb(112, 61, 170); font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">Int</span><span class="" style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">) -&gt;&nbsp;</span><span class="" style="color: rgb(112, 61, 170); font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">Int`</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><font face="georgia, serif" class="">, the `closure` is escaping other than non-escaping.</font></span></div></span></div></div></blockquote><div class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">No. The closure is not escaping. At least as far as I can see. Can you point out where it is escaping? Declaring a closure as escaping does not make it so. It only *allows* it to escape.</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Note that here, “escaping” means escaping the scope where it is declared, i.e. the `</font>mainFunction<span style="font-family: georgia, serif;" class="">` function. Clearly, it does not.</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="font-family: georgia, serif;" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="font-family: georgia, serif;" class=""><br class=""></span></div></span></div></div></div></span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class=""><div class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><blockquote type="cite" style="font-family: Helvetica; font-size: 12px;" class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="font-variant-ligatures: no-common-ligatures;"><font face="georgia, serif" class="">&nbsp;The result is not calculated inside the function `</font></span><font face="georgia, serif" class="">closureDoubled`</font><span class="" style="font-family: georgia, serif;">, it was calculated at `</span><font face="georgia, serif" class="">return temp1+temp2`, that is what I think can explain the behavior.&nbsp;</font></div></span></div></div></blockquote><div class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"></span></div></div></div></span></div></span></div></div></div></div></span></div></span></div></div></div><div class=""><br class=""></div><div class="">Where does this speculation comes from? It totally contradicts the story that the source code tells. temp1 and temp2 and Int variables that are local to `<span style="font-family: menlo; font-size: 11px;" class="">mainFunction</span>&nbsp;`, while `result` is local to the nested function `closureDoubled`. Unless you use of the word “result” does not refer to the `result` variable. I apologise for the poor choice of variable names that made possible this confusion.</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">I don't know why at first. It just like there were two ways to do the job, you thought it worked in this way, but it chose the other way. Both ways lead to the same result.</font></div></span></div></div></blockquote><div class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">You lost me in&nbsp;that sentence.&nbsp;What do the pronouns refer to?</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><blockquote type="cite" class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Then I changed your code to&nbsp;</font></div></span></div></div></blockquote><div class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">yes you totally changed my code by adding a closure parameter to the nested function, in which you passed the closure that was passed to the outer function.</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">By doing so, however, you altered significantly my use case. This is because the point of having a nested function, as opposed to a separate function defined outside the outer function, is for the nested function to capture the environment of its containing function.</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Of course, this begs the question: this is semantically equivalent, so why objecting to that? There are two answers to that question:</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">1- preventing a nested function from capturing a local&nbsp;closure is a significant limitation which essentially demotes closure from the status of first-class citizen. The artificial limitation of expressiveness in the language is unwarranted and a serious flaw (if intended. I still believe this is a bug)</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">2- A very common pattern for nested functions is to make recursive algorithms more memory-efficient by forwarding the recursion to a nested function whose parameters are only those values that change during the recursion. Other values, such as the parameter you just added in your change, if passed as parameters, will only waste stack space since they will never change in the recursion. This will also degrade performance slightly because the code will have to push this unchanging value to the stack every time a recursive call is made.</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Note that this was the context where I was bitten by the problem. Of course, you could expect a smart compiler to recognise that is doesn’t have to push unchanging parameter values onto the stack in recursive calls. Similarly, there are smart compiler which recognise terminal recursion and transform recursion into iteration. I even encountered in the past (Lisp) compilers which automatically transformed simple cases of non-terminal recursion into terminal-recursive versions (by adding an accumulator parameter), followed by transformation into iterative code. I do not expect typical compilers to do that (where “typical” is left undefined, but would include GCC, Clang, Swift). This is why I tend to implement my recursive calls in the way just described.</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><blockquote type="cite" style="font-family: Helvetica;" class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Everything works as expected now.</font></div></span></div></div></blockquote><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Expected, perhaps. As intended, certainly not.</font></div><br class=""><blockquote type="cite" style="font-family: Helvetica;" class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""> So I think the reason is just because of `closure` was not define in `func closureDoubled` in the first code&nbsp;snippet. It was defined outside, so it was escaping. What do you think?</font></div></span></div></div></blockquote><div class=""><div dir="ltr" class=""><div class="gmail_default"><span class="" style="font-variant-ligatures: no-common-ligatures;"><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div></span></div></div></div></font></div></span></div></div></div></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">I think this explanation does not make sense to me. Maybe I am missing something. Could you possibly detail what you mean?</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Thank you very much for your attempts. It’s possible something is escaping me (pun intended). But I still believe this is a bug in the compiler, if not in the language.</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Best regards.</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class="">Jean-Denis</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="georgia, serif" class=""><br class=""></font></div></span></div></div></div><div><blockquote type="cite" class=""><div class="">On 10 Oct 2016, at 02:45, 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"><font face="georgia, serif" class="">I changed you code `</font><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">let</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="">&nbsp;result = closure(n)` </span><span style="font-variant-ligatures:no-common-ligatures" class=""><font face="georgia, serif" class="">to</font></span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class=""> `<span style="font-family:menlo;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">let</span><span style="font-family: menlo; font-variant-ligatures: no-common-ligatures;" class=""> result = closure(</span><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</span><span style="font-family: menlo; font-variant-ligatures: no-common-ligatures;" class="">)</span>`</span><span style="font-variant-ligatures:no-common-ligatures" class=""><font face="georgia, serif" class="">. I thought that should eliminate the error. It didn't. The compiler asked me to change the whole code as below:</font><br class=""></span></div><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures" class=""><font face="georgia, serif" class=""><br class=""></font></span></div><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> mainFunction(closure: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">@escaping</span><span style="font-variant-ligatures:no-common-ligatures" class=""> (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</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="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> closureDoubled(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">_</span><span style="font-variant-ligatures:no-common-ligatures" class=""> n: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</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; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> result = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</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; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">2</span><span style="font-variant-ligatures:no-common-ligatures" class="">*result</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="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp1 = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</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><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">closureDoubled</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</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><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp1+temp2</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;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><font face="georgia, serif" class="">So I think in `</font></span><span style="color:rgb(186,45,162);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="">func</span><span style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">&nbsp;closureDoubled(</span><span style="color:rgb(186,45,162);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="">_</span><span style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">&nbsp;n:&nbsp;</span><span style="color:rgb(112,61,170);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="">Int</span><span style="font-family: menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">) -&gt;&nbsp;</span><span style="color:rgb(112,61,170);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="">Int`</span><span style="font-variant-ligatures:no-common-ligatures" class=""><font face="georgia, serif" class="">, the `closure` is escaping other than non-escaping. The result is not calculated inside the function `</font></span><font face="georgia, serif" class="">closureDoubled`</font><span style="font-family: georgia, serif;" class="">, it was calculated at `</span><font face="georgia, serif" class="">return temp1+temp2`, that is what I think can explain the behavior.&nbsp;</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">I don't know why at first. It just like there were two ways to do the job, you thought it worked in this way, but it chose the other way. Both ways lead to the same result.</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">Then I changed your code to&nbsp;</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class=""><br class=""></font></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> mainFunction2(closure:&nbsp; (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</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="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> closureDoubled(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">_</span><span style="font-variant-ligatures:no-common-ligatures" class=""> n: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">, closure2:(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</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; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> result = closure2(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</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; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">2</span><span style="font-variant-ligatures:no-common-ligatures" class="">*result</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="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp1 = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</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><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">closureDoubled</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</span><span style="font-variant-ligatures:no-common-ligatures" class="">, closure2: closure)</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><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp1+temp2</span></div><p style="margin:0px;line-height:normal" class="">









</p><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; line-height: normal;" class=""><font face="georgia, serif" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">Everything works as expected now. So I think the reason is just because of `closure` was not define in `func closureDoubled` in the first code&nbsp;snippet. It was defined outside, so it was escaping. What do you think?</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">Zhaoxin</font></div></span></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Oct 10, 2016 at 8:07 AM, Jean-Denis Muys 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 dir="ltr" class="">Here is a contrived reduction of my problem<br class=""><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> mainFunction(closure:(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</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; 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;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> closureDoubled(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">_</span><span style="font-variant-ligatures:no-common-ligatures" class=""> n: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</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; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> result = closure(n)</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="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">2</span><span style="font-variant-ligatures:no-common-ligatures" class="">*result</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; 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;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp1 = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</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><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">closureDoubled</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1</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><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp1+temp2</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><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class="">The line "<span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">let</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures" class="">&nbsp;result = closure(n)</span>" is refused by the compiler with the error message "declaration over non closing parameter may allow it to escape".</div><div class=""><br class=""></div><div class="">First off, while I know what an escaping or a non-escaping closure is, I find this error message utterly impossible to understand. To begin with, the sentence "non closing parameter" is meaningless to me.</div><div class=""><br class=""></div><div class="">In any case, my main function is passed a non-escaping closure. I want to call it from inside it, the compiler is ok with. I want also to call it from a nested function, but the compiler disagrees.</div><div class=""><br class=""></div><div class="">I believe the compiler should not complain here. Did I miss anything?</div><span class="HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class="">Jean-Denis</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></font></span></div>
<br class="">______________________________<wbr 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/<wbr class="">mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></div></body></html>