<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hey everyone.</div><div class=""><br class=""></div><div class="">I’m having some trouble with the following code, more precisely I’m getting the error "<font face="Courier New" class="">Cannot convert value of type '([Self]?, Error?) -> Void?' to expected argument type ‘([SomeModel]?, Error?) -> Void?’”</font> when trying to call <font face="Courier New" class="">“find2(withBlock: ([SomeModel]?, Error?) -> Void?)”</font> inside my extension.</div><div class=""><br class=""></div><div class="">Here’s a snippet:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Courier New" class="">protocol SomeModel { }</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">func find2(withBlock block: ([SomeModel]?, Error?) -> Void?) {</font></div><div class=""><font face="Courier New" class=""> print(#function)</font></div><div class=""><font face="Courier New" class="">}</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">protocol PersistentModel {</font></div><div class=""><font face="Courier New" class=""> typealias FindBlock = (_ objs: [Self]?, _ error: Error?) -> Void?</font></div><div class=""><font face="Courier New" class=""> func find(withBlock block: FindBlock)</font></div><div class=""><font face="Courier New" class="">}</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">extension PersistentModel where Self: SomeModel {</font></div><div class=""><font face="Courier New" class=""> func find(withBlock block: FindBlock) {</font></div><div class=""><font face="Courier New" class=""> find2(withBlock: block)</font></div><div class=""><font face="Courier New" class=""> }</font></div><div class=""><font face="Courier New" class="">}</font></div><div class=""><br class=""></div></div><div class="">Considering I’m constraining my protocol extension to <font face="Courier New" class="">SomeModel</font>, why do I get the error when trying to call “<span style="font-family: 'Courier New';" class="">find(withBlock: ([SomeModel]?, Error?) -> Void?)</span>”? It’s seems to me the type checker is not comparing the parameters' type inside the block and its inheritances/protocol conformances.</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class=""><div class=""><font face="Courier New" class="">class MyClass: SomeModel { }</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">func test(a: SomeModel) { }</font></div><div class=""><font face="Courier New" class="">test(a: MyClass())</font></div></div><div class=""><br class=""></div><div class=""><div class="">I’m not sure if I’m doing something wrong or if it is just a compiler/language limitation, any thoughts?</div></div><div class=""><br class=""></div><div class="">Thanks</div><div class=""><br class=""></div><div class="">- Henrique</div>
<br class=""></div></body></html>