<div dir="ltr">In the proposal I just posted in reply to Jordan Rose all these examples would work as expected, though the syntax and/or semantics would be slightly different:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">Example 1<br></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">In R.swift</blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">protocol R { func f() } // f is abstract</blockquote></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">In P.swift</blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">protocol P {} // f cannot be in protocol and extension</blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">extension P { func f() {... } } // f is implemented and declared to be in P</blockquote></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">In S.swift<br></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">struct S: R, P {} // f is from P whether an instance of S is types as S, P, or R<br> // If there was a spelling mistake for f in either P or R it would be detected since not all methods would be implemented<br><br><br></blockquote></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">Example 2<br></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"></blockquote></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">In P.swift<br></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">protocol P {} // f cannot be in protocol and extension<br>extension P where Self: AnyObject { func f() {... } } // f is implemented and declared to be in P if Self is an Object, otherwise abstract in P<br></blockquote></blockquote></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">In C:.swift<br></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">class C: P {} // Picks up f's implementation since Self is an object<br><br><br></blockquote></blockquote></blockquote>Would this be an acceptable solution - it is minorly source breaking, but I feel all solutions will be.</div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 17 May 2017 at 08:05, Slava Pestov via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">It’s not clear if such a keyword can be designed in a way that covers all cases. For example, consider a protocol extension that provides a default implementation for a requirement in a different protocol:<br>
<br>
protocol R {<br>
func f()<br>
}<br>
<br>
protocol P {}<br>
<br>
extension P {<br>
func f()<br>
}<br>
<br>
struct S : R, P {}<br>
<br>
Or a constrained extension that provides a default that only applies in certain cases:<br>
<br>
protocol P {<br>
func f()<br>
}<br>
<br>
extension P where Self : AnyObject {<br>
func f()<br>
}<br>
<br>
class C : P {}<br>
<br>
Slava<br>
<div class="HOEnZb"><div class="h5"><br>
> On May 16, 2017, at 8:53 AM, Johannes Weiss via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
><br>
> Hi swift-users,<br>
><br>
> Is there anything like the `override` keyword for making sure that default implementations in protocols are not adding a new function?<br>
><br>
> An example would be the following:<br>
><br>
> protocol FooProto {<br>
> func foo()<br>
> }<br>
><br>
> extension FooProto {<br>
> func foo() { /* <-- can I mark this as being a default implementation */<br>
> print("foo default")<br>
> }<br>
> }<br>
><br>
> Right now, there's a `func foo()` default implementation for `FooProto` but if later of `foo` gets refactored to `bar`, we lose the default implementation which can lead to problems.<br>
><br>
> Is there anything in Swift (like the `override` keyword) that allows me to say this is a default implementation and not a new method?<br>
><br>
> Thanks,<br>
> Johannes<br>
> ______________________________<wbr>_________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>