[swift-users] Compiler refuses non-escaping closure calls in nested function

Jean-Denis Muys jdmuys at gmail.com
Sun Oct 9 19:07:33 CDT 2016


Here is a contrived reduction of my problem

func mainFunction(closure:(Int) -> Int) -> Int {


    func closureDoubled(_ n: Int) -> Int {

        let result = closure(n)

        return 2*result

    }


    let temp1 = closure(1)

    let temp2 = closureDoubled(1)

    return temp1+temp2

}

The line "let result = closure(n)" is refused by the compiler with the
error message "declaration over non closing parameter may allow it to
escape".

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.

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.

I believe the compiler should not complain here. Did I miss anything?

Jean-Denis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161010/54f05504/attachment.html>


More information about the swift-users mailing list