[swift-users] withoutActuallyEscaping example question

Ray Fix rayfix at gmail.com
Sun Mar 26 02:13:08 CDT 2017


One of the motivating examples for withoutActuallyEscaping looks like the following.  This predictably  doesn’t work because "use of non-escaping parameter 'predicate' may allow it to escape"

func myFilter(array: [Int], predicate: (Int) -> Bool) -> [Int] {
  return Array(array.lazy.filter { predicate($0) })

The solution is to use withoutActuallyEscaping.  This works and produces the expected results.

func myFilter(array: [Int], predicate: (Int) -> Bool) -> [Int] {
  return withoutActuallyEscaping(predicate) { predicate in

What I find puzzling is the below example compiles and runs correctly. It seems like it should be the same compiler error as in the first example.

func myFilter(array: [Int], predicate: (Int) -> Bool) -> [Int] {
  return Array(array.lazy.filter(predicate))

If you understand why this is so, it would be very helpful.

Thank you and best wishes,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170326/bc49a1f3/attachment.html>

More information about the swift-users mailing list