I'm writing a InputStream (aka NSInputStream) subclass, which has an optional delegate that has one optional function:
public protocol StreamDelegate : NSObjectProtocol {
optional func stream(_ aStream: Stream, handle eventCode: Stream.Event)
}
When I use the delegate directly, I use the "proper" form of the function, which has a second named label of "handle". But when I unwrap the function, the signature appears to have changed, and Xcode tells me I need to remove the label. [Note: did not try running this code at all.]
guard let delegate = inputStream.delegate, let stream = delegate.stream else { return }
delegate.stream?(fetcher, handle: .openCompleted) // OK
stream(fetcher, handle: .openCompleted) // error: extraneous argument label 'handle:' in call
What's the reason for this (and/or is the compiler wrong)?
Optional protocol is not a problem in this case.
But binding a method into a variable causes this issue.
func myFunc(_ a: Int, withLabel b: String) {
print(b, a)
}
myFunc(1, "label") //Missing argument label 'withLabel:' in call
myFunc(1, withLabel: "label") //valid
let f = myFunc
f(1, "label") //valid
f(1, withLabel: "label") //Extraneous argument label 'withLabel:' in call
In this example, the type of f is a closure type `(Int, String) -> Void`, and in the current Swift, Closures cannot have labels.
github.com/apple/swift-evolution/blob/master/proposals/0111-remove-arg-label-type-significance.md