Hey everyone.
I’m having some trouble with the following code, more precisely I’m getting the
error "Cannot convert value of type '([Self]?, Error?) -> Void?' to expected
argument type ‘([SomeModel]?, Error?) -> Void?’” when trying to call
“find2(withBlock: ([SomeModel]?, Error?) -> Void?)” inside my extension.
Here’s a snippet:
protocol SomeModel { }
func find2(withBlock block: ([SomeModel]?, Error?) -> Void?) {
print(#function)
}
protocol PersistentModel {
typealias FindBlock = (_ objs: [Self]?, _ error: Error?) -> Void?
func find(withBlock block: FindBlock)
}
extension PersistentModel where Self: SomeModel {
func find(withBlock block: FindBlock) {
find2(withBlock: block)
}
}
Considering I’m constraining my protocol extension to SomeModel, why do I get
the error when trying to call “find(withBlock: ([SomeModel]?, Error?) ->
Void?)”? It’s seems to me the type checker is not comparing the parameters'
type inside the block and its inheritances/protocol conformances.
Just for testing purposes I created a simple class implementing the protocol
and a function with the parameter of the same type, we can see the problem does
not occur, being strictly linked to closures.
class MyClass: SomeModel { }
func test(a: SomeModel) { }
test(a: MyClass())
I’m not sure if I’m doing something wrong or if it is just a compiler/language
limitation, any thoughts?
Thanks
- Henrique
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users