0
0
Swiftprogramming~10 mins

Autoclosures (@autoclosure) in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Autoclosures (@autoclosure)
Call function with expression
@autoclosure wraps expression
Function receives closure
Closure executed inside function
Expression evaluated now
Function returns result
The @autoclosure attribute wraps an expression into a closure automatically, delaying its evaluation until used inside the function.
Execution Sample
Swift
func logIfTrue(_ predicate: @autoclosure () -> Bool) {
    if predicate() {
        print("It's true!")
    }
}

logIfTrue(2 > 1)
This code delays evaluating '2 > 1' until inside the function, then prints if true.
Execution Table
StepActionExpression/ConditionClosure Called?Output
1Call logIfTrue with 2 > 12 > 1No
2@autoclosure wraps '2 > 1' into closureClosure createdNo
3Inside function, call predicate()Evaluate 2 > 1Yes
4Condition is truetrueYesIt's true! printed
5Function ends
💡 Function ends after printing because condition is true
Variable Tracker
VariableStartAfter Step 2After Step 3Final
predicatenoneclosure wrapping '2 > 1'closure called, evaluates to trueclosure called, true
Key Moments - 2 Insights
Why isn't the expression '2 > 1' evaluated immediately when calling the function?
Because @autoclosure wraps the expression into a closure, delaying evaluation until predicate() is called inside the function (see execution_table step 2 and 3).
What happens if the closure is never called inside the function?
The expression is never evaluated, so no output or side effects occur (see execution_table step 1 and 2 where closure is created but not called).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the expression '2 > 1' actually evaluated?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Check the 'Closure Called?' column where evaluation happens inside the function.
According to the variable tracker, what does 'predicate' hold after step 2?
AThe boolean value true
BAn integer value
CA closure wrapping '2 > 1'
DNothing (nil)
💡 Hint
Look at the 'After Step 2' column for 'predicate' in variable_tracker.
If the function never calls predicate(), what will be the output?
AIt prints "It's false!"
BNo output
CIt prints "It's true!"
DCompilation error
💡 Hint
See key_moments about what happens if closure is not called.
Concept Snapshot
Syntax: Use @autoclosure before a function parameter.
Behavior: Wraps an expression into a closure automatically.
Evaluation: Expression is delayed until closure is called inside the function.
Use: Simplifies syntax for delayed evaluation without explicit closure syntax.
Example: func f(_ p: @autoclosure () -> Bool) { if p() { ... } }
Full Transcript
Autoclosures in Swift let you pass an expression to a function without writing a closure explicitly. The expression is wrapped automatically into a closure and only evaluated when the function calls it. This delays evaluation and can improve readability. In the example, calling logIfTrue(2 > 1) does not evaluate '2 > 1' immediately. Instead, the expression is wrapped into a closure by @autoclosure. Inside the function, calling predicate() runs the expression and returns true, so the message prints. If the closure is never called, the expression is never evaluated and no output occurs. This helps avoid unnecessary work or side effects until needed.