What is @autoclosure and when is it useful?
Source: interviewadvanced
WHAT IT TESTS: understanding deferred, lazy evaluation. OUTLINE: @autoclosure wraps an argument expression in a closure so it evaluates lazily only if used, enabling clean APIs like assert and the ?? operator.
WHAT IT TESTS: whether you understand lazy argument evaluation and its readability cost. ANSWER OUTLINE: @autoclosure automatically wraps a passed expression in a closure, so the expression is not evaluated until the function invokes it; this powers assert, precondition, and the ?? operator, which skip work when not needed; pairing it with @escaping allows storing the closure.
Read the original → interview
- #ios
- #swift
- #autoclosure
- #closures
- #lazy-evaluation
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.