robnik
October 4, 2020, 3:54pm
1
This seems weird. Is it considered a bug?
import os
let log: Logger = ...
class SomeClass {
var foo = 1
func someMethod() {
log.info("Blah blah \(foo)") // error
}
}
Error:
Reference to property 'foo' in closure requires explicit use of 'self' to make capture semantics explicit
I thought maybe the autoclosure caused it, but it must be more than that because this compiles fine:
func other(_ s: @autoclosure () -> String) {}
func someMethod() {
other("Blah blah \(foo)")
}
Lantua
October 4, 2020, 6:43pm
2
What used there is not a normal @autoclosure
. It's an @escaping @autoclosure
.
robnik
October 4, 2020, 7:04pm
3
If I add the @escaping
to my example other
function, I can still use it without the explicit self.
func other(_ s: @escaping @autoclosure () -> String) {}
func someMethod() {
other("Blah blah \(foo)") // still compiles
}
Lantua
October 4, 2020, 7:08pm
4
Hmm, that's weird. I'd expect it to throw the same error, especially that foo
does escape . That other
on behaviour actually sounds like a bug. Also, you (correctly) got the error in this case:
func otherInt(_ s: @escaping @autoclosure () -> Int) { }
func someMethod() {
otherInt(foo) // error
}
Do you mind filing a bug report?
To show that foo
escapes:
var x: () -> String = { "" }
class SomeClass {
var _storage = 0
var foo: Int {
_storage += 1
return _storage
}
func someMethod() {
other("\(foo)")
}
}
func other(_ s: @escaping @autoclosure () -> String) {
x = s
}
SomeClass().someMethod()
x() // 1
x() // 2