<div dir="ltr"><div class="gmail_default"><font face="georgia, serif">&gt; It is 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 class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">I think it is because the compiler **flatten** your inner function instead of calculated the result.</font></div><div class="gmail_default"><br></div><div class="gmail_default"><font face="georgia, serif">The variable scope of `closure` is outside the `func closureDoubled`, so it is `escaping` from that function. However, since `closure` is defined outside, there is no place in</font><span style="font-family:georgia,serif"> </span><span style="font-family:georgia,serif">`func closureDoubled`</span><span style="font-family:georgia,serif"> mark as `@escaping`, so the compiler asked me to add `@escaping` in the place where it is firstly defined.</span></div><div class="gmail_default"><span style="font-family:georgia,serif"><br></span></div><div class="gmail_default"><span style="font-family:georgia,serif">Zhaoxin</span></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif"> </font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 10, 2016 at 4:06 PM, Jean-Denis Muys <span dir="ltr">&lt;<a href="mailto:jdmuys@gmail.com" target="_blank">jdmuys@gmail.com</a>&gt;</span> wrote:<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">It seems to me you are shooting while blindfolded.<div><br></div><div><div><blockquote type="cite"><div dir="ltr"><div class="gmail_default"><font face="georgia, serif">I changed you code `</font><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> result = closure(n)` </span><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">to</font></span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> `<span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> result = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</span><span style="font-variant-ligatures:no-common-ligatures">)</span>`</span><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">. I thought that should eliminate the error. It didn&#39;t. The compiler asked me to change the whole code as below:</font><br></span></div></div></blockquote></div><div><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif"><br></font></span></div><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">Why did you expect changing which argument to send the closure to make any difference?</font></span></div></div></div><div><br></div><div><blockquote type="cite"><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> mainFunction(closure: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">@<wbr>escaping</span><span style="font-variant-ligatures:no-common-ligatures"> (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div></span></div></div></blockquote></div><div><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span 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 style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"><blockquote type="cite" style="font-family:Helvetica;font-size:12px"><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">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">func</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> closureDoubled(</span><span style="color:rgb(186,45,162);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">_</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> n: </span><span style="color:rgb(112,61,170);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">Int</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="color:rgb(112,61,170);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">Int`</span><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">, the `closure` is escaping other than non-escaping.</font></span></div></span></div></div></blockquote><div><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">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 style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">Note that here, “escaping” means escaping the scope where it is declared, i.e. the `</font>mainFunction<span style="font-family:georgia,serif">` function. Clearly, it does not.</span></div><div style="margin:0px;line-height:normal"><span style="font-family:georgia,serif"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-family:georgia,serif"><br></span></div></span></div></div></div></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"><div><div><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><blockquote type="cite" style="font-family:Helvetica;font-size:12px"><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif"> The result is not calculated inside the function `</font></span><font face="georgia, serif">closureDoubled`</font><span style="font-family:georgia,serif">, it was calculated at `</span><font face="georgia, serif">return temp1+temp2`, that is what I think can explain the behavior. </font></div></span></div></div></blockquote><div><div dir="ltr"><div class="gmail_default"><span 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><br></div><div>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">mainFunction</span> `, 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><br></div><div><blockquote type="cite"><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="georgia, serif">I don&#39;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><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">You lost me in that sentence. What do the pronouns refer to?</font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><blockquote type="cite"><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="georgia, serif">Then I changed your code to </font></div></span></div></div></blockquote><div><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">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 style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">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 style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">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 style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">1- preventing a nested function from capturing a local 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 style="margin:0px;line-height:normal"><font face="georgia, serif">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 style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">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 style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><blockquote type="cite" style="font-family:Helvetica"><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="georgia, serif">Everything works as expected now.</font></div></span></div></div></blockquote><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">Expected, perhaps. As intended, certainly not.</font></div><br><blockquote type="cite" style="font-family:Helvetica"><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="georgia, serif"> So I think the reason is just because of `closure` was not define in `func closureDoubled` in the first code snippet. It was defined outside, so it was escaping. What do you think?</font></div></span></div></div></blockquote><div><div dir="ltr"><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div></span></div></div></div></font></div></span></div></div></div></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">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 style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">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 style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">Best regards.</font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">Jean-Denis</font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div></span></div></div></div><div><blockquote type="cite"><div>On 10 Oct 2016, at 02:45, Zhao Xin &lt;<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>&gt; wrote:</div><br class="m_-3624585465419828925Apple-interchange-newline"><div><div dir="ltr"><div class="gmail_default"><font face="georgia, serif">I changed you code `</font><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> result = closure(n)` </span><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">to</font></span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> `<span style="font-family:menlo;font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures"> result = closure(</span><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</span><span style="font-family:menlo;font-variant-ligatures:no-common-ligatures">)</span>`</span><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">. I thought that should eliminate the error. It didn&#39;t. The compiler asked me to change the whole code as below:</font><br></span></div><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif"><br></font></span></div><div class="gmail_default"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> mainFunction(closure: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">@escaping</span><span style="font-variant-ligatures:no-common-ligatures"> (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><p 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 class="m_-3624585465419828925webkit-block-placeholder"></p><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:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> closureDoubled(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> n: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> result = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</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:rgb(186,45,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">2</span><span style="font-variant-ligatures:no-common-ligatures">*result</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><p 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 class="m_-3624585465419828925webkit-block-placeholder"></p><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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> temp1 = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> temp2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">closureDoubled</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</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:rgb(186,45,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> temp1+temp2</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"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">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">func</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> closureDoubled(</span><span style="color:rgb(186,45,162);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">_</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> n: </span><span style="color:rgb(112,61,170);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">Int</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="color:rgb(112,61,170);font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures">Int`</span><span style="font-variant-ligatures:no-common-ligatures"><font face="georgia, serif">, the `closure` is escaping other than non-escaping. The result is not calculated inside the function `</font></span><font face="georgia, serif">closureDoubled`</font><span style="font-family:georgia,serif">, it was calculated at `</span><font face="georgia, serif">return temp1+temp2`, that is what I think can explain the behavior. </font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">I don&#39;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"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">Then I changed your code to </font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> mainFunction2(closure:  (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><p 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 class="m_-3624585465419828925webkit-block-placeholder"></p><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:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> closureDoubled(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> n: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">, closure2:(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> result = closure2(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</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:rgb(186,45,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">2</span><span style="font-variant-ligatures:no-common-ligatures">*result</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><p 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 class="m_-3624585465419828925webkit-block-placeholder"></p><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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> temp1 = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</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:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> temp2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">closureDoubled</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</span><span style="font-variant-ligatures:no-common-ligatures">, closure2: closure)</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:rgb(186,45,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> temp1+temp2</span></div><p style="margin:0px;line-height:normal">









</p><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;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">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 snippet. It was defined outside, so it was escaping. What do you think?</font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">Zhaoxin</font></div></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 10, 2016 at 8:07 AM, Jean-Denis Muys via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Here is a contrived reduction of my problem<br><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:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> mainFunction(closure:(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><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="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> closureDoubled(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> n: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</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:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> result = closure(n)</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:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">2</span><span style="font-variant-ligatures:no-common-ligatures">*result</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="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> temp1 = closure(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</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:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> temp2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">closureDoubled</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1</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:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> temp1+temp2</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><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>The line &quot;<span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-family:menlo;font-size:11px;font-variant-ligatures:no-common-ligatures"> result = closure(n)</span>&quot; is refused by the compiler with the error message &quot;declaration over non closing parameter may allow it to escape&quot;.</div><div><br></div><div>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 &quot;non closing parameter&quot; is meaningless to me.</div><div><br></div><div>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><br></div><div>I believe the compiler should not complain here. Did I miss anything?</div><span class="m_-3624585465419828925HOEnZb"><font color="#888888"><div><br></div><div>Jean-Denis</div><div><br></div><div><br></div><div><br></div></font></span></div>
<br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></blockquote></div><br></div>