New Loggers + String interpolation requires explicit self?

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)")
}

What used there is not a normal @autoclosure. It's an @escaping @autoclosure.

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
}

Hmm, that's weird. I'd expect it to throw the same error, especially that foo does escape :thinking:. 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
Terms of Service

Privacy Policy

Cookie Policy