Implicit Autoclosure Shorthand

I was thinking about auto closures and was curious this shorthand would be good:

func assert(_ condition: @autoclosure Bool, _ label: @autoclosure String) { ... }

Where it is treated like this code:

func assert(_ condition: @autoclosure () -> Bool, _ label: @autoclosure () -> String) { ... }

This would allow for the implication that a @autoclosure affected type would be assumed as () -> T when T is not a closure.

I want to know why this has not been implemented yet? Is it a technical limitation or does it conflict with the swift design guidelines.

1 Like

This condition is likely the reason why. It would be inconsistent and confusing that @autoclosure Bool means @autoclosure () -> Bool, but @autoclosure () -> Bool does not mean @autoclosure () -> () -> Bool. Especially since autoclosures are rarely used and should be avoided in most cases.

3 Likes

Consistent application of such a rule would mean that the interpretation of something like

func foo<T>(_ x: @autoclosure T)

would depend on whether T is a closure type or not at the call site.

1 Like