Skip to content
tezvyn:

What is @autoclosure and when is it useful?

Source: interviewHardHow cards are made

Summary

understanding deferred, lazy evaluation.

Key points

@autoclosure wraps an argument expression in a closure so it evaluates lazily only if used, enabling clean APIs like assert and the ?? operator.

What's really being asked

This checks deep language knowledge: do you understand that arguments are normally evaluated eagerly, and that @autoclosure changes that contract by deferring evaluation? It also tests judgment, because the feature trades performance and expressiveness for reduced transparency.

The full answer

Normally Swift evaluates a function argument before the call. Marking a parameter @autoclosure tells the compiler to automatically wrap the argument expression in a zero-argument closure, so the expression runs only when the function body actually calls that closure. This enables APIs where the work should be skipped if unnecessary. The standard library uses it for assert(_:_:), where the message and condition are not computed in release builds, and for the ?? operator, where the right-hand default is evaluated only when the left side is nil. Combine it with @escaping when the closure must outlive the call, for example to log lazily.

The mistakes people make

Describing it as just shorthand for passing a closure with empty braces. Ignoring the trade-off and recommending it broadly. Forgetting that callers can no longer tell the argument is deferred, so side effects may run later or never.

What usually comes next

How does @autoclosure interact with @escaping? Why is it dangerous when the expression has side effects? How does the ?? operator use it? What is the performance benefit in assert?

A concrete example

Consider func logIfEnabled(_ message: @autoclosure () -> String). Calling logIfEnabled(expensiveDescription()) does not run expensiveDescription unless logging is enabled, because the function decides whether to invoke the closure. Without @autoclosure the caller would have to write { expensiveDescription() }, and without lazy evaluation the expensive call would always run even when its result is discarded.

Interview question

What is the primary effect of marking a Swift function parameter with @autoclosure?

  • a.It wraps the argument expression in a closure so it evaluates lazily only when the function invokes itCorrect
  • b.It automatically retains the argument to prevent deallocation
  • c.It forces the argument expression to evaluate before the call for thread safety
  • d.It allows the parameter to accept any type without generics
Why?

@autoclosure defers evaluation by wrapping the expression in a closure the function may or may not call, as with assert and ??. It does not force eager evaluation, manage retention, or relax type checking.

Just read this? Test yourself on what you have been reading.

Read the original → docs.swift.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles