<div dir="ltr">On Wed, Aug 24, 2016 at 3:39 AM, Jonathan Hull <span dir="ltr">&lt;<a href="mailto:jhull@gbis.com" target="_blank">jhull@gbis.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><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"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On Aug 23, 2016, at 8:35 AM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">On Tue, Aug 23, 2016 at 3:02 AM, Jonathan Hull<span> </span><span dir="ltr">&lt;<a href="mailto:jhull@gbis.com" target="_blank">jhull@gbis.com</a>&gt;</span><span> </span>wrote:<br><div class="gmail_extra"><div class="gmail_quote"><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"><div style="word-wrap:break-word"><br><div><span><blockquote type="cite"><div>On Aug 22, 2016, at 11:32 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br><div><div dir="ltr">On Mon, Aug 22, 2016 at 11:59 PM, Jonathan Hull via swift-evolution<span> </span><span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-<wbr>evolution@swift.org</a>&gt;</span><span> </span>wrote:<br><div class="gmail_extra"><div class="gmail_quote"><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">Hi everyone,<br><br>We talked about this before when we were discussing mixins, and there seemed to be generally positive feelings towards it as a feature for the future.</blockquote><div><br></div><div>It&#39;s been some time now since the original discussion, so perhaps you could refresh our collective memory (or at least, mine): although it *seems* like this feature might be useful, I can&#39;t recall a concrete use case where I&#39;ve felt like I needed this feature--do you have some examples?</div></div></div></div></div></blockquote><div><br></div></span><div>Ideally, the biggest use is that it helps to (partially) solve the diamond problem (and similar issues) by forcing/allowing disambiguation when there are multiple protocols being conformed to.  This will become more of an issue if we allow protocols or extensions to add storage.  Your proposed syntax actually does a better job of it than mine because mine was always shown as attached to some sort of implementation, whereas yours could potentially allow access to a default implementation under a new name.</div><div><br></div><div>Other than that, it generally allows us to bypass/mitigate conflicts between protocols.  In the current version, you are unable to conform to both protocols (either because it won’t compile or because you can’t satisfy the semantics of both protocols) without designing the protocols together to avoid conflicts.  (I have definitely had to go back and rename/refactor properties on a protocol for this reason… which I couldn’t have done if I didn’t control both protocols).</div></div></div></blockquote><div><br></div><div>I understand something of the difficulty of confronting the diamond problem. As I wrote above, I&#39;m inclined to believe that this proposed feature would help solve a real issue. However, the point I&#39;m trying to make is that, on reflection, I have never actually been hampered by the lack of this feature, and so I&#39;d like to continue the discussion to get a fuller sense of just how impactful this proposal would be, both positive and negative.</div><div><br></div><div>It&#39;s true, of course, that if you control at least one of two protocols (you don&#39;t need to control both protocols), it is trivially easy to cause this problem to occur, but as you point out it is also possible to resolve the problem by re-designing the protocol you control. I&#39;m inclined to think (without evidence, admittedly) that re-designing to remove the conflict, where possible, would actually be the superior option in most cases.</div><div><br></div><div>My question was: have you actually run into a scenario that necessitates the feature you propose because you controlled neither conflicting protocol? I think it would strengthen the proposal greatly to have a concrete, uncontrived example.</div></div></div></div></div></blockquote><div><br></div></span><div>Right now I commonly have to hand-namespace protocol methods/properties to avoid conflicts.  So instead of having ‘var image:UIImage’ (which is the name which makes the most sense in the protocol’s context), I have ‘var protocolNameImage:UIImage’.  There are lots of things which have common properties like ‘count’ which have to be called ‘somethingCount’ or ‘countOfSomething’.  In the context of the protocol, these names are full of redundant words (especially when measured against the new naming guidelines).  We are all used to doing this for Objective C, but it feels out of place in Swift.</div><div><br></div><div>This will become a much more serious issue as the third-party code ecosystem grows.  Without some capability like this, you will have frameworks which can’t be used together (or at least with the same object).  I would hate to see a ‘best practice’ emerge of adding 3 letter prefixes to all protocol methods to get around compatibility issues.</div></div></div></blockquote><div><br></div><div>Ah, well this isn&#39;t exactly the diamond problem you&#39;re talking about here. Instead, I think, we have a fundamental disagreement. I think I&#39;ve been told that this opinion of mine is &#39;insane&#39;--but I hold to it:</div><div><br></div><div>Protocols are not merely a vehicle for delivering a reusable bag of code. One of its most essential purposes is to constrain the shape or API of its conforming types. Therefore, it is a feature, not a bug, that with every choice of name in a protocol you foreclose the possibility of composing that protocol with others that might have colliding names.</div><div><br></div><div>Currently, if you the protocol vendor have made the decision that `image` &quot;makes the most sense in the protocol&#39;s context&quot;, you must have considered whether it would be absurd for a conforming type to have another use for `image`. If it would be absurd, then `image` is the appropriate name for your protocol requirement and any other word would truly be redundant. But, if this is only one of many plausible images, then `somethingImage` or `imageOfSomething` *is* the appropriate name, and trying to shorten the name isn&#39;t at all consistent with Swift guidelines but rather an incorrect attempt to prioritize brevity over clarity.</div><div><br></div><div>What you&#39;re arguing is that protocol designers should be able to design protocols without regard for how they will compose with others in conforming types, relying on a new member-renaming feature instead. But, as you point out, you can already use a protocol as a mere bag of code by naming all members with unique, prefixed names, then have conforming types forward their own choice of names to these.</div><div><br></div><div>This member-renaming feature you propose would enhance the aesthetic pleasure of the protocol designer, allowing simple names that don&#39;t ever have to appear in the public API of a concrete type to be used for a protocol member without placing any restrictions on the API of conforming types. However, I don&#39;t see anything wrong with the current hand-prefixing method being enshrined as &quot;best practice&quot; for the bag-of-code approach to protocols. If, as you predict, a growing third-party code ecosystem makes name collisions worse, then in fact having uniquely distinguishable prefixed members would be less confusing than having conforming types renaming protocol members as a matter of course.</div><div><br></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"><div style="word-wrap:break-word"><div><span class=""><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_extra"><div class="gmail_quote"><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"><div style="word-wrap:break-word"><div><div>Take a look at Eiffel’s ‘rename’ &amp; ’select’ features for similar functionality and use-cases.</div><div><br></div><div>Ultimately, this is a step in the direction of having true mixins.</div><span><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div></div></div></div></div></div></blockquote></span></div></div></blockquote><div><br></div><div>Sure, maybe. I couldn&#39;t evaluate that claim. I&#39;m inclined to favor the proposal, but it&#39;d have to stand on its own merits, not as a step to an as-yet undesigned feature.</div></div></div></div></div></blockquote><div><br></div></span><div>This would be part of that design.  We have to start somewhere.  A journey of 1000 miles begins with a single step.</div><span class=""><div><br></div><div><br></div><br><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_extra"><div class="gmail_quote"><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"><div style="word-wrap:break-word"><div><span><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>   I am fairly certain this affects the ABI though, so I thought I would bring it up now.</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"><br>If two protocols have methods/properties with the same name, but different signatures, we need a way to distinguish between them when attempting to conform to both.<br><br>       <span> </span>protocol A {<br>               <span> </span>var x:Int {get set}<br>       <span> </span>}<br><br>       <span> </span>protocol B {<br>               <span> </span>var x:Double {get set}<br>       <span> </span>}<br></blockquote><div><br></div><div>Methods can be overloaded that differ in arguments or return type, so it seems like this problem mainly exists with *properties* that differ in type--am I wrong?</div></div></div></div></div></blockquote><div><br></div></span><div>There is also the case of functions with the same name and signature, but different semantics.  There may be no single implementation which simultaneously satisfies the semantics for both protocols. By renaming one of the functions, we are able to provide separate implementations for each requirement (which allows both protocols to function as intended).</div></div></div></blockquote><div><br></div><div>True. However, putting on my critical hat, this seems like we&#39;re stretching to provide support for an anti-pattern. It&#39;d be nice to have an example where one runs into the motivating problem *and* where the proposed feature promotes a _better_ design than is currently possible, rather than making a bad design compile.</div><div><br></div><div>At this point, I&#39;m imagining scenarios where a user is trying to conform a type MyAnimal to both Biped and Quadruped, then worrying that `walk()` has two semantics: something has already gone deeply wrong IMO.</div><div><br></div><div>[Yes, I know there are animals that can sometimes walk on two or four legs. The point here is that the protocols were clearly designed to model animals at a certain level of detail, while it appears that the user writing `MyAnimal` wants to model the animal at a different level of detail than either protocol was designed to handle. You might have specific qualms about this particular hypothetical, but I think you can pick out the general point that there is a much larger problem inherent in the design than the specific problem regarding two colliding method signatures.]</div></div></div></div></div></blockquote><div><br></div></span><div>To take your example of walk().  Perhaps we have a protocol ‘Walkable’ which refers to any data structure where the nodes can be walked using the ‘walk()’ function.  It is easy to imagine two different protocols A &amp; B which specialize on this in different ways (say LinearWalkable &amp; RandomWalkable), and both add some methods/properties and use those to provide efficient default implementations.  At some point, you may run into a data structure which could easily be walked in both ways.</div><div><br></div><div>As things are right now, you couldn’t inherit from both protocols.  While you could add new ‘linearWalk()’ &amp; ‘randomWalk()’ to the protocols respectively (cluttering their interface), there is still the issue of what to do when &#39;walk()’ is called.  You can’t rename walk() in the originating protocols because it comes from their common ancestor.  Much better to force one (or both) of the methods to be renamed on the conforming data structure.  That keeps the interfaces of the protocols clean and makes the options available on the data structure clearer (e.g. ‘walk()’ &amp; ‘randomWalk()’ )</div><div><br></div><div>What I have had to do in the current version is inherit from the original protocol and then copy and paste the default implementations from the specialized versions.  Now my code has been duplicated and is harder to maintain.  We can do better.</div><span class=""><br></span></div></div></blockquote><div><br></div><div>I think Charles&#39;s solution is pretty nice, but he&#39;s right that the API surface area will have to grow. I don&#39;t know his original use case, so I don&#39;t know how ugly I&#39;d find the final solution to be in that scenario. In this particular example, I&#39;d say that having `linearWalk()` and `randomWalk()` distinguished seems pretty sensible and an overall win for clarity. If the same vendor controlled all three protocols, then `Walkable` could have the `walk()` requirement removed altogether for even more clarity.</div><div><br></div><div>Also, just a hunch, but I suspect your hypothetical would never hold. Could you envision how the requirements for RandomWalkable might be such that it&#39;s possible to implement an efficient _default_ implementation of a random walk for any of several conforming data structures, but only one of these data structures is LinearWalkable, _and_ such a linear walk is efficient using another _default_implementation for an overlapping but not identical set of data structures? It&#39;s not mere trivia here, because the crux of your argument is that there exist default implementations that require copying and pasting into conforming types (and sufficiently efficient default implementations so that copying and pasting is appropriate rather than implementing a more efficient version). More likely in diamond problem scenarios, I think, colliding members are going to be properties or methods either without default implementations or than need to supply more efficient versions of default implementations anyway.</div><div><br></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"><div style="word-wrap:break-word"><div><span class=""><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_extra"><div class="gmail_quote"><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"><div style="word-wrap:break-word"><div><div>There may also be functions in different protocols with different names but the same semantics and signature.  This will allow a single implementation to satisfy both protocols without duplication.</div></div></div></blockquote><div><br></div><div>This is a poor argument IMO. You can already implement foo() and then have bar() forward to foo() with trivial effort and really minimal boilerplate. It&#39;s an existing solution, and a more general solution because it doesn&#39;t require matching signatures. Also, it&#39;s a better solution IMO because it preserves the notion that a type T : Fooable, Barrable provides the full API guaranteed by Fooable and Barrable.</div><div><br></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"><div style="word-wrap:break-word"><div><div>Finally, we may want to rename an inherited default implementation to avoid conflicting with another protocol&#39;s default implementation in cases where we don’t want to override it.</div></div></div></blockquote><div><br></div><div>Yes, I think this would be handy. I can&#39;t think of an existing way to do this, and I expect it might make a big difference in designing good protocols. So here, I think we have a strong argument.</div><div><br></div><div>Again, though, could we find a concrete example of how this feature would promoter a better design of an actual type and/or protocol?</div><div><br></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"><div style="word-wrap:break-word"><div><span><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> One possibility is to allow a struct/class/enum to conform to the protocol while renaming one (or both) of the clashing methods:</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"><br>       <span> </span>struct C: A,B {<br>               <span> </span>var x:Int<br>               <span> </span>var y:Double implements B.x<br>       <span> </span>}<br><br>The conforming method/property would still have to have the same signature, but could have a different name (and parameter labels).  It would also allow protocol methods which have identical signatures and semantics, but different names to be implemented using the same method (i.e ‘implements D.z &amp; E.w’).<br><br>When something is cast to the protocol (say ‘as B’), then calling the property (e.g. ‘x’) would end up calling the implementation of the renamed property ( ‘y’ in this example) on the conforming type.<br></blockquote><div><br></div><div>Reflecting on this proposed change, it occurs to me that something of value would be lost, and I think that this something is actually rather valuable:</div><div><br></div><div>Today, when I see that a type conforms to (for example) Sequence, I know that certain methods and/or properties exist on that type. Protocol conformance guarantees a certain API, not just certain semantics.</div></div></div></div></div></blockquote><div><br></div></span><div>It isn’t actually lost, however.  When working with it as a Sequence (for example), that API would be intact using the original names.  It is only when working with it as its own type that the renaming would have an effect.</div></div></div></blockquote><div><br></div><div>That is not my point. With this proposal, knowing that MyGreatType conforms to Sequence would no longer yield any information as to the MyGreatType API. That is definitely something lost and we should acknowledge that.</div><div><br></div><div>Also, recall that Sequence has Self or associated type requirements. So:</div><div><br></div><div>```</div><div>let m = MyGreatType()</div><div>// There is nothing I can write here to use the Sequence API with `m`, IIUC;</div><div>// however, depending on how this feature is designed, I *might* be able to</div><div>// call a generic function that operates on a type `T : Sequence` and work</div><div>// with `m` that way.</div><div>```</div></div></div></div></div></blockquote><div><br></div></span><div>I get what you are saying, but this is really asking for an Xcode feature to better show the generated interfaces.  Code completion will still work properly.</div><span class=""><div><br></div><div><br></div><br><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_extra"><div class="gmail_quote"><div><br></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"><div style="word-wrap:break-word"><div><span><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Perhaps one way to mitigate this loss would be to have any renamed members listed *in the declaration of conformance*, something like this (with some additional bikeshedding):</div><div><br></div><div>```</div><div>struct MyGreatType : Sequence (count =&gt; length) {</div><div> <span> </span>// MyGreatType conforms to Sequence but renames `count` to `length`</div><div>}</div><div>```</div></div></div></div></blockquote><div><br></div></span><div>Yes, putting it in the conformance declaration is a definite possibility we should consider.</div><span><div><br></div><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></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">I think we would also want a way to retroactively conform using existing properties/methods in an extension declaring conformance.  Not sure what the best syntax for that would be.  Off the top of my head (though I would love to have something with less cruft):<br><br>       <span> </span>extension D:B {<br>               <span> </span>@conform(to: B.x, with: D.y)<br>       <span> </span>}<br><br>or maybe just:<br><br>       <span> </span>extension D:B {<br>               <span> </span>D.y implements B.x<br>       <span> </span>}<br></blockquote><div><br></div><div>If renamed members are declared along with protocol conformance, then the syntax for retroactive modeling follows naturally:</div><div><br></div><div>```</div><div>extension D : B (x =&gt; y) { }</div><div>// again, the actual notation here is ugly</div><div>// but the underlying idea, I think, is worth considering</div><div>```</div></div></div></div></div></blockquote><div><br></div></span><div>Yup</div><div><br></div><div>One thing I like about this is that it helps to solve the diamond problem.  ‘x’ could be a default implementation in B which D does not override.  I think this is an important case which my original proposal didn’t address fully.</div><div><br></div><div>We should keep bikeshedding the syntax though...</div></div><br><div>Thanks,</div><div>Jon</div></div></blockquote></div></div></div></div></blockquote></span></div><br></div></blockquote></div><br></div></div>