I'm sure we've all been there, trying to concisely print all the elements of an array on a new line:
[1, 2, 3].forEach(print)
But that doesn't compile. On the otherhand, if you explicitly make a closure, it'll work:
[1, 2, 3].forEach { print($0) }
It looks semantically equivalent, but it's not. The print($0
)` call in the explicit closure:
- populates the default arguments of
print(Any..., separator: String, terminator: String)
- by the absence of the
to: inout Target
argument, it disambiguates it fromprint<Target>(Any..., separator: String, terminator: String, to: inout Target)
- Handles the conversion from
Int
toAny...
Seems desirable to have the compiler automatically generate a thunk that serves the purpose of this explicit closure.