<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="">I totally agree.</div><div class=""><br class=""></div><div class="">Yes this limitation only applies to methods declared in the protocol, that are always dynamically dispatched.</div><div class=""><br class=""></div><div class="">For functions that are only declared in the protocol extension, but not in the protocol itself, you can use the static dispatch:</div><div class=""><br class=""></div><div class=""><div class=""> protocol P {</div><div class=""> func dyn()</div><div class=""> }</div><div class=""><br class=""></div><div class=""> extension P {</div><div class=""> func dyn() {</div><div class=""> print("P.dyn")</div><div class=""> }</div><div class=""> func ext() {</div><div class=""> print("P.ext")</div><div class=""> }</div><div class=""> }</div><div class=""><br class=""></div><div class=""> struct C : P {</div><div class=""> func dyn() {</div><div class=""> // No way to call P.dyn because of dynamic dispatch</div><div class=""> print("C.dyn")</div><div class=""> }</div><div class=""> func ext() {</div><div class=""> (self as P).ext()</div><div class=""> print("C.ext")</div><div class=""> }</div><div class=""> }</div><div class=""><br class=""></div><div class=""> C().ext() // prints P.Ext, C.ext</div><div class=""><br class=""></div></div><div class="">Gwendal Roué</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">Le 8 déc. 2015 à 18:43, Mateusz Zajac via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 14px;">If you define a simple protocol like:</div><div class="panel code" style="margin: 9px 0px; padding: 0px; border: 1px solid rgb(204, 204, 204); background-color: rgb(245, 245, 245); line-height: 1.33333333333333; font-family: monospace; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: rgb(51, 51, 51);"><div class="codeContent panelContent" style="margin: 0px; padding: 9px 12px;"><pre class="code-java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal;">protocol Foo {
func testPrint()
}
</pre></div></div><p class="" style="margin: 10px 0px 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 14px;">And than you provide a default implementation for testPrint() method in protocol extensions like:</p><div class="panel code" style="margin: 9px 0px; padding: 0px; border: 1px solid rgb(204, 204, 204); background-color: rgb(245, 245, 245); line-height: 1.33333333333333; font-family: monospace; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: rgb(51, 51, 51);"><div class="codeContent panelContent" style="margin: 0px; padding: 9px 12px;"><pre class="code-java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal;">extension Foo {
func testPrint() {
print(<span class="code-quote" style="color: rgb(0, 145, 0);">"Protocol extension call"</span>)
}
}
</pre></div></div><p class="" style="margin: 10px 0px 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 14px;">You aren't allowed to call the fault implementation from the structure eg.</p><div class="panel code" style="margin: 9px 0px; padding: 0px; border: 1px solid rgb(204, 204, 204); background-color: rgb(245, 245, 245); line-height: 1.33333333333333; font-family: monospace; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: rgb(51, 51, 51);"><div class="codeContent panelContent" style="margin: 0px; padding: 9px 12px;"><pre class="code-java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal;">struct Bar: Foo {
func testPrint() {
self.testPrint()
print(<span class="code-quote" style="color: rgb(0, 145, 0);">"Call from struct"</span>)
}
}
</pre></div></div><p class="" style="margin: 10px 0px 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 14px;">This is some sort of limitation as often happens that a default implementation is providing a major part of implementation and only one, simple line is changed in actual struct implementation. If you're using classes you can achieve this by creating a base class and calling a method on super. If you consider structs, there's no such possibility and you always have to write a whole implementation from scratch in each structure which conforms to the protocol.</p><hr class="" style="color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 14px;"><p class="" style="margin: 10px 0px 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Arial, sans-serif; font-size: 14px;">You can use composition by creating nested structure, but it's neither logical nor clean... It's rather a hack...</p><div class="panel code" style="margin: 9px 0px; padding: 0px; border: 1px solid rgb(204, 204, 204); background-color: rgb(245, 245, 245); line-height: 1.33333333333333; font-family: monospace; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: rgb(51, 51, 51);"><div class="codeContent panelContent" style="margin: 0px; padding: 9px 12px;"><pre class="code-java" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; max-height: 30em; overflow: auto; white-space: pre-wrap; word-wrap: normal;">struct Bar: Foo {
func testPrint() {
<span class="code-comment" style="color: rgb(128, 128, 128);">// Calling <span class="code-keyword">default</span> implementation
</span> struct Dummy : Foo {}
let dummy = Dummy()
dummy.testPrint()
print(<span class="code-quote" style="color: rgb(0, 145, 0);">"Call from struct"</span>)
}
}</pre></div></div><div class=""><br class=""></div><img alt="" width="1" height="1" border="0" class="" style="border-width: 0px !important; margin: 0px !important; padding: 0px !important;" apple-inline="yes" id="A96DFDC2-D608-4E75-B13D-C42E6E0D1D35" apple-width="yes" apple-height="yes" src="cid:D7DFCBEB-1ED9-4155-97F2-FA788E5D14B9"></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote><div class=""><div class=""><br class=""></div></div></div></body></html>