Hi,

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
    Array(array.lazy.filter({predicate($0)}))
  }
}

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

Reply via email to