Consider code like this:
func log(_ msg: @autoclosure @escaping () -> Any?) {
print(msg)
}
class Foo {
let bar = 3
let lazy: String
init() {
log("bar is \(self.bar)")
self.lazy = "always late"
}
}
This does not compile:
Error: 'self' captured by a closure before all members were initialized
Fair enough: even though there clearly is no problem here, the compiler can't be expected to figure out that (arbitrary) closures don't use self
in ways that are undefined (yet).
Of course, I can work around this by doing:
let bar = self.bar
log("bar is \(bar)")
But this seems clumsy.
Is there a way to tell the Swift compiler to evaluate the @autoclosure
-parameter upfront, that is in essence to ignore @autoclosure
?
PS: I copied this signature from XCGLogger. I'm not sure why @autoclosure
is needed there.