0Pricing
Swift Academy · Lesson

Autoclosures

Delay expression evaluation with @autoclosure.

What Is an Autoclosure?

@autoclosure automatically wraps an argument expression in a closure, so it is not evaluated until the function actually uses it. The caller writes a plain expression, not a closure.

Without Autoclosure

Normally delayed evaluation needs an explicit closure at the call site:

func logIf(_ cond: Bool, _ message: () -> String) {
    if cond { print(message()) }
}
logIf(true) { "expensive" }  // caller writes braces

All lessons in this course

  1. Capturing Values by Reference
  2. Escaping Closures
  3. Capture Lists and Weak Self
  4. Autoclosures
← Back to Swift Academy