<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 8, 2015 at 1:11 AM, Kevin Ballard via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">I&#39;d love to see these two key things that Joe mentioned get added.<br>
Diagnostics around confusing situations is a good idea (although there<br>
needs to be a way to disable it too; what if I intentionally need to use<br>
the same name in a type and in an extension to one of the conformed-to<br>
protocols, for various reasons?).<br>
<br>
I can&#39;t support the original proposal though, of making foo.bar() use<br>
dynamic dispatch. The ability to use static dispatch in most cases is<br>
one of the great features of Swift, and it&#39;s something that needs to be<br>
preserved, not relegated to being an optimization that can be done<br>
occasionally.<br>
<br>
It&#39;s also worth noting that the current behavior, where methods defined<br>
in protocol extensions can&#39;t be overridden by the implementing type, may<br>
actually be the desired behavior in many cases.</blockquote><div> </div><div>It is indeed the only logical choice; for example I could make a private protocol extension - obviously, a class that has no access to it won&#39;t be able to override it.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Heck, the ability to<br>
trigger this different behavior is one of the three reasons to even<br>
define methods in protocol extensions instead of in the protocol (the<br>
others being when you&#39;re defining it for a subset of protocol<br>
implementations, and when you&#39;re defining extensions on protocols<br>
imported from other modules). Removing this functionality (by making<br>
foo.bar() always dynamic) means reverting back to the Swift 1.x<br>
convention of having global functions to provide this behavior.<br>
<br>
I wouldn&#39;t mind seeing something like the `dynamic` keyword to allow<br>
concrete types to override these protocol extension methods (although<br></blockquote><div><br></div><div>You currently allow it by declaring method to be part of protocol, which means that an entry for some method is reserved in the protocol witness table. This entry will be filled either by concrete method, if present, or by protocol default implementation.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
`dynamic` in this case would not imply @objc, like it does today, which<br>
may be a little confusing), but that would be opt-in behavior.<br>
<br>
-Kevin Ballard<br>
<div class=""><div class="h5"><br>
On Mon, Dec 7, 2015, at 12:14 PM, Joe Groff via swift-evolution wrote:<br>
&gt; The main reason to constrain dynamic dispatch is modularity. The problems<br>
&gt; of interfering categories or monkey-patches in languages like ObjC and<br>
&gt; Ruby with late-bound dispatch and open classes are well-known. In Swift&#39;s<br>
&gt; model, it isn&#39;t possible for an extension in one module to interfere with<br>
&gt; existing protocol conformances or class hierarchies at runtime (except<br>
&gt; with @objc of course). Modules need to be compiled together to be aware<br>
&gt; of each other to interact with each other&#39;s interfaces.<br>
&gt;<br>
&gt; The particular example Alexandros brings up is more an artifact of our<br>
&gt; existing implementation than desirable behavior. Two key things are<br>
&gt; missing:<br>
&gt;<br>
&gt; - the ability for extensions to protocols to add new<br>
&gt; dynamically-dispatched methods, and<br>
&gt; - compiler quality work to diagnose confusing cases where concrete types<br>
&gt; obviously shadow non-dynamic protocol extensions.<br>
&gt;<br>
&gt; There is a related behavior change proposal Doug Gregor&#39;s been working on<br>
&gt; for classes, to have conforming to a protocol implicitly re-declare all<br>
&gt; the protocol&#39;s methods as class methods, so that they can be overridden<br>
&gt; by subclasses in the expected way.<br>
&gt;<br>
&gt; -Joe<br>
&gt;<br>
&gt; &gt; On Dec 6, 2015, at 8:17 PM, Paul Cantrell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt; &gt;<br>
&gt; &gt; One of the few things in Swift 2 that feels to me like a design flaw is the way Swift mixes static and dynamic method dispatch.<br>
&gt; &gt;<br>
&gt; &gt; Alexandros Salazar gives an excellent explanation of this problem — and I agree wholeheartedly with his title for the article:<br>
&gt; &gt;<br>
&gt; &gt;    <a href="http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future" rel="noreferrer" target="_blank">http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future</a><br>
&gt; &gt;<br>
&gt; &gt; The upshot is that when we see this:<br>
&gt; &gt;<br>
&gt; &gt;    foo.bar()<br>
&gt; &gt;<br>
&gt; &gt; …it’s very hard to know how the compiler will determine which implementation of bar() to use. It might use static dispatch; it might use dynamic dispatch.<br>
&gt; &gt;<br>
&gt; &gt; The rules that govern this are arcane, and hard to remember. They have the feeling of being a “gotcha” question for job interviews — always a red flag for language features.<br>
&gt; &gt;<br>
&gt; &gt; Even if you remember the rules, the information needed to determine whether dispatch is static or dynamic is hard to track down. It depends on whether bar()’s implementation comes from an extension, whether the extension method appeared on the extended protocol, and whether the inferred type of foo is the protocol itself or an implementing type.<br>
&gt; &gt;<br>
&gt; &gt; A crucial part of the meaning of “foo.bar()” is implicit, and hard to determine. This runs contrary to Swift’s stated goal of prioritizing clarity at the point of API use, and its general pattern of making intent explicit. And it feels dangerous — a wellspring of insidious bugs.<br>
&gt; &gt;<br>
&gt; &gt; Thus:<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; PROPOSAL<br>
&gt; &gt;<br>
&gt; &gt; Make the syntax “foo.bar()” always use dynamic dispatch, i.e. always use _only_ the runtime type of foo to determine which implementation of bar() to use. If an extension method collision occurs when a type implements multiple protocols, require the type to explicitly specify which one to use (as Swift already requires the caller to do at the point of invocation).<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; I mean this proposal somewhat as a strawman. It’s such an obvious choice, I’m sure there were good reasons not to do it. But I’d like to propose the obvious solution in order to understand what’s wrong with it. I realize static dispatch precludes some optimizations, but I doubt that this alone drove the design choice. I see no safety or expressiveness upside to the way it works now.<br>
&gt; &gt;<br>
&gt; &gt; Cheers,<br>
&gt; &gt;<br>
&gt; &gt; Paul<br>
&gt; &gt;<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; swift-evolution mailing list<br>
&gt; &gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt; &gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div></div>