<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 class="">Hi, Henrique. This is a correct error message. Consider this use case:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">struct Impl:&nbsp;PersistentModel {</div><div class="">&nbsp; func doTheThing()</div><div class="">}</div><div class=""><br class=""></div><div class="">func callback(_ objs: [Impl]?, _ error: Error?) -&gt; Void? {</div><div class="">&nbsp; return objs.first?.doTheThing()</div><div class="">}</div><div class="">find2(withBlock: callback) // direct call for clarity</div></blockquote><div class=""><br class=""></div><div class="">Clearly here the ‘callback’ function isn’t compatible with the type ‘find2’ expects—it wants to be able to pass an array of SomeModel to the function, but the function only handles Impl instances specifically. Any other conforming type would be a problem.</div><div class=""><br class=""></div><div class="">Hope that helps,</div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 1, 2016, at 14:48, Henrique Valcanaia via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div 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?) -&gt; Void?' to expected argument type&nbsp;‘([SomeModel]?, Error?) -&gt; Void?’”</font> when trying to call&nbsp;<font face="Courier New" class="">“find2(withBlock: ([SomeModel]?, Error?) -&gt; 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?) -&gt; Void?) {</font></div><div class=""><font face="Courier New" class="">&nbsp; &nbsp; 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="">&nbsp; &nbsp; typealias FindBlock = (_ objs: [Self]?, _ error: Error?) -&gt; Void?</font></div><div class=""><font face="Courier New" class="">&nbsp; &nbsp; 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="">&nbsp; &nbsp; func find(withBlock block: FindBlock) {</font></div><div class=""><font face="Courier New" class="">&nbsp; &nbsp; &nbsp; &nbsp;find2(withBlock: block)</font></div><div class=""><font face="Courier New" class="">&nbsp; &nbsp; }</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?) -&gt; 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></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>