I'm having issue with conforming UIView
to one of my protocols. Here's a demo of the protocol:
protocol MyProtocol {
func through(_ modifier: (Self) -> Void) -> Self
}
extension MyProtocol {
@discardableResult
func through(modifier: (Self) -> Void) -> Self {
modifier(self)
return self
}
}
As you can see, I'm using Self in the parameter of a parameter, which is a closure.
Before this Swift 5.8.1 release, I was able to extend UIView
to conform to it:
extension UIView: MyProtocol { }
Now the extension UIView: MyProtocol
code won't compile:
Protocol 'MyProtocol' requirement 'through(modifier:)' cannot be satisfied by a non-final class ('UIView') because it uses 'Self' in a non-parameter, non-result type position
The workaround here is simple. Just remove the requirement in the protocol definition while keep the implementation in the extension:
protocol MyProtocol {
// remove this line:
// func through(_ modifier: (Self) -> Void) -> Self
}
extension MyProtocol {
// and keep this:
@discardableResult
func through(modifier: (Self) -> Void) -> Self {
modifier(self)
return self
}
}
And it compiles.
Any reason why this happens?