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
_______________________________________________ swift-dev mailing list swift-dev@swift.org https://lists.swift.org/mailman/listinfo/swift-dev