<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Thank you, Ole. Your reply teaches me a lot.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 10, 2016 at 6:52 PM, Ole Begemann via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The line "let result = closure(n)" is refused by the compiler with the<br>
error message "declaration over non closing parameter may allow it to<br>
escape".<br>
<br>
First off, while I know what an escaping or a non-escaping closure is, I<br>
find this error message utterly impossible to understand. To begin with,<br>
the sentence "non closing parameter" is meaningless to me.<br>
</blockquote>
<br></span>
The error message I'm seeing in Xcode 8.0 is "Declaration closing over *non-escaping* parameter 'closure' may allow it to escape", so I don't know where you're seeing the "non closing parameter". And "non-escaping parameter" does make a lot more sense, I think.<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In any case, my main function is passed a non-escaping closure. I want<br>
to call it from inside it, the compiler is ok with. I want also to call<br>
it from a nested function, but the compiler disagrees.<br>
<br>
I believe the compiler should not complain here. Did I miss anything?<br>
</blockquote>
<br></span>
I think the error message is actually quite good, given that the compiler apparently is taking some shortcuts to prove that a parameter doesn't escape.<br>
<br>
By declaring a function that closes over the non-escaping parameter, you're creating a situation that *may* allow the non-escaping closure to escape, i.e. the compiler can't guarantee anymore that it won't escape. For example, you could do assign the `closureDoubled` function to a variable that's outside the scope of `mainFunction`:<br>
<br>
// Variable outside the scope of mainFunction<br>
var f: (Int) -> (Int) = { $0 }<span class=""><br>
<br>
func mainFunction(closure: (Int) -> Int) -> Int {<br>
<br>
func closureDoubled(_ n: Int) -> Int {<br>
let result = closure(n)<br>
return 2*result<br>
}<br>
<br></span>
// closure would escape here<br>
f = closureDoubled<br>
...<br>
}<br>
<br>
mainFunction { $0 }<br>
f(5)<br>
<br>
If this were allowed, `closure` would be able to escape. I think this possibility explains the error message.<br>
<br>
Now the compiler *could* of course check for this and I think you're right in arguing that it *should* ideally perform more sophisticated checks, but since it currently seems to be taking some shortcuts in guaranteeing that a parameter doesn't escape, it has to disallow anything it can't verify as correct.<br>
<br>
Ole<div class="HOEnZb"><div class="h5"><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>
</div></div></blockquote></div><br></div>