[swift-dev] Reason for call only non-escaping parameter

Dimitri Racordon Dimitri.Racordon at unige.ch
Wed May 31 14:21:43 CDT 2017


Hi everyone,

I failed to find the reason why Swift does not allows a non-escaping parameter to be assigned to a local variable. Here is a minimal example:

func f(_ closure: () -> Int) {
    let a = closure
}

I do understand that assigning a non-escaping closure to a variable whose lifetime exceeds that of the function would (by definition) violate the non-escaping property. For instance, doing that is understandably illegal:

var global = { 0 }
func f(_ closure: () -> Int) {
    global = closure
}

But in my first example, since `a` is stack allocated, there’s no risk that `closure` escapes the scope of `f`.

Is there some use case I’m missing, where such assignment could be problematic?
Or is this a limitation of the compiler, which wouldn't go all the way to check whether the lifetime of the assignee is compatible with that of the non-escaping parameter may exceed that of the variable it is assigned to?

Thank you very much for your time and your answer.

Best,
Dimitri Racordon

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20170531/46cf9a77/attachment.html>


More information about the swift-dev mailing list