In the swift document about autoclosure attribute to function type in function parameter, it says as following.
Apply this attribute to delay the evaluation of an expression by automatically wrapping that expression in a closure with no arguments.
That means the expression given as argument for autoclosure parameter of a function is not evaluated and encapsulated in a closure automatically. And the type of that closure must be () -> T, where the T is the type of given expression.
The following code illustrates what that means.
class A {
init() {print("Hi")}
deinit {print("Bye~")}
}
func f(a: @autoclosure () -> A) { print("f")}
func g(a: A) { print("g")}
do {
f(a: A())
print("-----------")
g(a: A())
}
The result is:
f
-----------
Hi
g
Bye~
We can see the A() expression for f is not evaluated at all, but for g, it is evaluated as an instance of A by call A.init() method before execute the body of g, and call A.deinit() after the execution of the body of g, seeing the Hi and Bye~ in the result.
Therefore we can get expression lazy evaluation by using @autoclosure on function parameter. If the evaluation of the arugment expression is quite expensive and not always needed, using @autoclosure would have performance gain.