Function in capture list: "extraneous argument label" error

115 views Asked by At
func customPrint(number: Int, via printingFunction: @escaping (Int) -> Void) {
    printingFunction(number)
}


class Temp {
    func tempPrintingFunction(number i: Int) {
        print(i)
    }

    func print5() {
        customPrint(number: 5) { [self] number in
            tempPrintingFunction(number: number)
        }
    }
}

Temp().print5()

This code is fine: compiles and works as expected.
But when I pass function itself in capture list instead of self:

func print5() {
    customPrint(number: 5) { [tempPrintingFunction] number in
        tempPrintingFunction(number: number)
    }
}

I get the error:

Extraneous argument label 'number:' in call

When I remove the label, everything backs to normal.
But it's weird. What is it? Bug or feature? I can't find any information about this neither in documentation nor on forums.

Bonus question:
Why doesn't the code crash, when I pass unowned self to the capture list? Shouldn't it crash? I want it to crash

Edited:
By the way, is there any difference in this situation between capturing self and function itself? In terms of retain cycles, etc. If we capture function, self will be captured as well, isn't it?

0

There are 0 answers