<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="">There’s no semantic difference between static &amp; dynamic dispatch here. Static dispatch is an optimization the compiler can make.</div><div class=""><br class=""></div><div class="">(I’m assuming that compete() is secretly module-scoped, as Joe said it would be.)</div><div class=""><br class=""></div><div class="">P</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 7, 2015, at 5:04 PM, ilya &lt;<a href="mailto:ilya.nikokoshev@gmail.com" class="">ilya.nikokoshev@gmail.com</a>&gt; wrote:</div><div class=""><br style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">On Tuesday, December 8, 2015, Paul Cantrell via swift-evolution &lt;</span><a href="mailto:swift-evolution@swift.org" style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><span style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">&gt; wrote:</span><br style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote class="gmail_quote" style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">&gt; The ability to use static dispatch in most cases is one of the great features of Swift, and it's something that needs to be preserved, not relegated to being an optimization that can be done occasionally.<br class=""><br class="">Can you think of a situation where that’s the desired semantics, as opposed to either a desirable optimization or a confusing workaround for the “monkey collision” problem? (I can’t see the use case, but I’m open to hearing it!)<br class=""></blockquote><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Sure, private protocol extension. For example,&nbsp;</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><span style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">public protocol Animal {</span><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">&nbsp; func greet()</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">&nbsp; func sit()</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">&nbsp; func run()</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">}</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">private extension Animal {</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">&nbsp; func compete() { greet(); sit(); run()}</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">}</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><span style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">// we are provided with animal&nbsp;</span><span style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></span><br style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">... animal.compete()...</div><div style="font-family: HelveticaNeue; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""><div class=""><br class=""></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;">&gt; It's also worth noting that the current behavior, where methods defined in protocol extensions can't be overridden by the implementing type, may actually be the desired behavior in many cases.<br class=""><br class="">If you don’t want a method to be overridable, that’s what “final” is for.<br class=""><br class="">The current behavior, in which a method is silently shadowed, does not seem to be to be a desirable behavior in any situation. The compiler warnings/errors Joe Groff proposed seem like a bare minimum, but I’d prefer to eliminate the situation that needs to be warned about in the first place.<br class=""><br class="">Cheers, P<br class=""><br class="">&gt; On Dec 7, 2015, at 4:11 PM, Kevin Ballard via swift-evolution &lt;<a class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;<br class="">&gt; I'd love to see these two key things that Joe mentioned get added.<br class="">&gt; Diagnostics around confusing situations is a good idea (although there<br class="">&gt; needs to be a way to disable it too; what if I intentionally need to use<br class="">&gt; the same name in a type and in an extension to one of the conformed-to<br class="">&gt; protocols, for various reasons?).<br class="">&gt;<br class="">&gt; I can't support the original proposal though, of making foo.bar() use<br class="">&gt; dynamic dispatch. The ability to use static dispatch in most cases is<br class="">&gt; one of the great features of Swift, and it's something that needs to be<br class="">&gt; preserved, not relegated to being an optimization that can be done<br class="">&gt; occasionally.<br class="">&gt;<br class="">&gt; It's also worth noting that the current behavior, where methods defined<br class="">&gt; in protocol extensions can't be overridden by the implementing type, may<br class="">&gt; actually be the desired behavior in many cases. Heck, the ability to<br class="">&gt; trigger this different behavior is one of the three reasons to even<br class="">&gt; define methods in protocol extensions instead of in the protocol (the<br class="">&gt; others being when you're defining it for a subset of protocol<br class="">&gt; implementations, and when you're defining extensions on protocols<br class="">&gt; imported from other modules). Removing this functionality (by making<br class="">&gt; foo.bar() always dynamic) means reverting back to the Swift 1.x<br class="">&gt; convention of having global functions to provide this behavior.<br class="">&gt;<br class="">&gt; I wouldn't mind seeing something like the `dynamic` keyword to allow<br class="">&gt; concrete types to override these protocol extension methods (although<br class="">&gt; `dynamic` in this case would not imply @objc, like it does today, which<br class="">&gt; may be a little confusing), but that would be opt-in behavior.<br class="">&gt;<br class="">&gt; -Kevin Ballard<br class="">&gt;<br class="">&gt; On Mon, Dec 7, 2015, at 12:14 PM, Joe Groff via swift-evolution wrote:<br class="">&gt;&gt; The main reason to constrain dynamic dispatch is modularity. The problems<br class="">&gt;&gt; of interfering categories or monkey-patches in languages like ObjC and<br class="">&gt;&gt; Ruby with late-bound dispatch and open classes are well-known. In Swift's<br class="">&gt;&gt; model, it isn't possible for an extension in one module to interfere with<br class="">&gt;&gt; existing protocol conformances or class hierarchies at runtime (except<br class="">&gt;&gt; with @objc of course). Modules need to be compiled together to be aware<br class="">&gt;&gt; of each other to interact with each other's interfaces.<br class="">&gt;&gt;<br class="">&gt;&gt; The particular example Alexandros brings up is more an artifact of our<br class="">&gt;&gt; existing implementation than desirable behavior. Two key things are<br class="">&gt;&gt; missing:<br class="">&gt;&gt;<br class="">&gt;&gt; - the ability for extensions to protocols to add new<br class="">&gt;&gt; dynamically-dispatched methods, and<br class="">&gt;&gt; - compiler quality work to diagnose confusing cases where concrete types<br class="">&gt;&gt; obviously shadow non-dynamic protocol extensions.<br class="">&gt;&gt;<br class="">&gt;&gt; There is a related behavior change proposal Doug Gregor's been working on<br class="">&gt;&gt; for classes, to have conforming to a protocol implicitly re-declare all<br class="">&gt;&gt; the protocol's methods as class methods, so that they can be overridden<br class="">&gt;&gt; by subclasses in the expected way.<br class="">&gt;&gt;<br class="">&gt;&gt; -Joe<br class="">&gt;&gt;<br class="">&gt;&gt;&gt; On Dec 6, 2015, at 8:17 PM, Paul Cantrell via swift-evolution &lt;<a class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;&gt;&gt;<br class="">&gt;&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 class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; Alexandros Salazar gives an excellent explanation of this problem — and I agree wholeheartedly with his title for the article:<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt;&nbsp;<span class="Apple-converted-space">&nbsp;</span><a href="http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future" target="_blank" class="">http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future</a><br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; The upshot is that when we see this:<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt;&nbsp; foo.bar()<br class="">&gt;&gt;&gt;<br class="">&gt;&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 class="">&gt;&gt;&gt;<br class="">&gt;&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 class="">&gt;&gt;&gt;<br class="">&gt;&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 class="">&gt;&gt;&gt;<br class="">&gt;&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 class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; Thus:<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; PROPOSAL<br class="">&gt;&gt;&gt;<br class="">&gt;&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 class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt;<br class="">&gt;&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 class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; Cheers,<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; Paul<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; _______________________________________________<br class="">&gt;&gt;&gt; swift-evolution mailing list<br class="">&gt;&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><a class="">swift-evolution@swift.org</a><br class="">&gt;&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">&gt;&gt;<br class="">&gt;&gt; _______________________________________________<br class="">&gt;&gt; swift-evolution mailing list<br class="">&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><a class="">swift-evolution@swift.org</a><br class="">&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">&gt; _______________________________________________<br class="">&gt; swift-evolution mailing list<br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a class="">swift-evolution@swift.org</a><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a class=""></a><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></blockquote></div></div></blockquote></div><br class=""></body></html>