O'Reilly logo

Swift 3 ObjectOriented Programming - Second Edition by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing equivalent closures with simplified code

It is possible to omit the type for the closure's parameter and return type. The following lines show a simplified version of the previously shown code that generates the same result. Note that the closure code is really simplified and doesn't even include the return statement because it uses implicit return. Swift evaluates the code we write after the in keyword and returns its evaluation as if we included the return statement before the expression. Swift infers the return type. We just have to replace the existing code for the filteredBy method in the NumbersWorker class with the new code. The code file for the sample is included in the swift_3_oop_chapter_07_10 folder:

 open func filteredBy(condition: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required