Improving Closure Hygiene

Each closure starts with an optional signature declaration that concludes with the in keyword. It’s followed by statements that form the closure’s body. Closures expressions may include any or all of the following components:

 {
  [capture-list] (parameters and their types) ​throws​ -> result-type ​in
  statements
 }

Add declaration elements reluctantly, preferring to include just those items required to make your closure compile and function properly. Any element that can be inferred often should be inferred. After that, follow your in-house standards regarding additional declaration items. Most Swift developers omit return and parameter types where allowed. A short signature is generally both readable and useful. ...

Get Swift Style, 1st Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.