<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On 17 Sep 2016, at 05:32, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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="">On Fri, Sep 16, 2016 at 20:28 Karl &lt;<a href="mailto:razielim@gmail.com" target="_blank" class="">razielim@gmail.com</a>&gt; wrote:<br class=""><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;" class=""><div class=""><blockquote type="cite" class=""><div class="">On 17 Sep 2016, at 01:45, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class=""></blockquote></div></div><div style="word-wrap: break-word;" class=""><div class=""><blockquote type="cite" class=""><div class="">I absolutely agree that it's a problem worth solving. However, the question is whether a particular proposed design solves the problem and avoids previously stated weaknesses. What I'm saying here is that, so far, the conversation in this thread has involved reiterating already-proposed designs that have been critiqued. It's really quite tiring for me too to repeat the same critique when someone raises the same proposal a second or third time.<br class=""><br class="">It's possible that it makes sense to have a separate syntax for retroactive modeling. I haven't been able to come up with one that seems reasonable to me, or I would have written to this list to propose it. Do you have such a design in mind?<br class=""><div class="gmail_quote"><div dir="ltr" class="">On Fri, Sep 16, 2016 at 16:59 Charles Srstka &lt;<a href="mailto:cocoadev@charlessoft.com" target="_blank" class="">cocoadev@charlessoft.com</a>&gt; wrote:<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;"><div style="word-wrap: break-word;" class="">On Sep 16, 2016, at 4:08 PM, Xiaodi Wu via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><div class=""><blockquote type="cite" class=""><br class=""><div class=""><span 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; float: none; display: inline;" class="">We've had this discussion on the list multiple times already. The gist of the difficulty here is that most proposals for a mandatory keyword don't permit retroactive modeling, so it's a no-go. On the other hand, the core team seems to take a dim view to optional syntax, since that's more in the ballpark of linters.</span></div></blockquote></div><br class=""></div><div style="word-wrap: break-word;" class=""><div class="">Numerous solutions to your objection have been proposed; you always simply dismiss all of them in favor of your dogmatic stance. It’s really quite tiring. You can have this and support retroactive modeling; you just might need to have a separate syntax for retroactive conformances. You keep bringing that up as a hard-and-fast objection, but you know what? Maybe retroactive conformances<span class="Apple-converted-space">&nbsp;</span><b class="">should</b>&nbsp;have a separate syntax, because they’re not saying the same thing! One is saying "here are some methods that will make this type conform to this protocol”, where the other is saying “this type already has the methods that conform to this protocol somewhere.” These are not the same thing, and it might be confusing to see a conformance declaration and assume it’s the former when it’s actually the latter, and then have trouble finding the conformances. Maybe it would actually make your code clearer if retroactive conformances were required to declare “this method exists somewhere else already.” Maybe you could even command-click on it and jump to the actual declaration. Anything would be better than the current situation, because:</div><div class=""><br class=""></div><div class="">The reason this keeps coming up is because it’s a real problem. I myself have started taking up the practice of always using copy-and-paste to declare conformances to protocols, because otherwise the chances of mistyping something and having the bug not manifest itself until runtime is simply too high. This is not a “linter” problem; this affects basic functionality and makes protocols, honestly, really dangerous to use. For a language that bills itself as “protocol-oriented”, it’s really quite damning that its protocol support is this brittle and fragile compared to its support for traditional inheritance. I’ve been bitten by this enough times by now to somewhat regret the decision to go with a protocol-based design. This is a real shame because conceptually, the idea of Swift’s protocol-based design is really cool.</div></div><div style="word-wrap: break-word;" class=""><div class=""><br class=""></div><div class="">Charles</div><div class=""><br class=""></div></div></blockquote></div></div></blockquote></div></div><div style="word-wrap: break-word;" class=""><div class=""><blockquote type="cite" class=""><div class="">______________________________<wbr class="">_________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" 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/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class=""></div></blockquote></div></div><div style="word-wrap: break-word;" class=""><div class=""><blockquote type="cite" class=""><div class=""></div></blockquote></div><br class=""><div class=""><br class=""></div><div class="">I don’t see what the big problem about retroactive modelling is.</div></div></blockquote><div class=""><br class=""></div><div class="">Let me give a concrete example of how retroactively modeling is used. Currently, there is a JIRA bug that Set does not conform to SetAlgebra. To fix this issue, someone simply needs to write `extension Set : SetAlgebra { }` and some tests. That's literally what the bug (filed by a core team member) tells you to do. It's a starter bug, and if someone hasn't taken it yet, you the reader could have a go at it. What's neat about Swift is that it's super easy to provide the same functionality in your own project without waiting on that bug to be fixed in Swift itself. You can simply write a single line of code.&nbsp;By contrast, if your proposal were to be implemented, this would become much more difficult.</div><div class=""><br class=""></div><div class="">This is actively used in Swift today. For example, in the Swift implementation of NSScanner, you'll find the following lines:</div><div class=""><br class=""></div><div class="">```</div><div class=""><div class="">internal protocol _BitShiftable {</div><div class="">&nbsp; &nbsp; static func &gt;&gt;(lhs: Self, rhs: Self) -&gt; Self</div><div class="">&nbsp; &nbsp; static func &lt;&lt;(lhs: Self, rhs: Self) -&gt; Self</div><div class="">}</div><div class=""><br class=""></div><div class="">internal protocol _IntegerLike : Integer, _BitShiftable {</div><div class="">&nbsp; &nbsp; init(_ value: Int)</div><div class="">&nbsp; &nbsp; static var max: Self { get }</div><div class="">&nbsp; &nbsp; static var min: Self { get }</div><div class="">}</div><div class=""><br class=""></div><div class="">extension Int : _IntegerLike { }</div><div class="">extension Int32 : _IntegerLike { }</div><div class="">extension Int64 : _IntegerLike { }</div><div class="">extension UInt32 : _IntegerLike { }</div><div class="">extension UInt64 : _IntegerLike { }</div></div><div class="">```</div><div class=""><br class=""></div><div class="">If we adopted your proposed syntax below, it would take considerably more lines of boilerplate code to express the same thing. The burden increases significantly with the complexity of the retroactive modeling. For instance, if the retroactively modeled protocol had 20 requirements and you were retroactively conforming 20 types, that'd be at least 400 lines of boilerplate.</div><div class="">&nbsp;</div></div></div></div></blockquote><div><br class=""></div><div>Yes, this is necessarily going to be a big chunk of boilerplate, because there are n integer types with no useful common ancestors. We could come up with all kinds of shorthands for making this easier, but there are basically two situations when adding a conformance of a new protocol P when there are existing members with the same names:</div><div><br class=""></div><div>- You’re adapting the semantics of new protocol P to your internal way of working. You need a more advanced bridge between the two models which may require maintenance as your internal workings change. Nothing much we can do about that.</div><div>- You’re ensuring capabilities which may be provided by other protocols. You want to forward to the implementation of P.member to your an existing protocol’s X.member because they have the same semantics.</div><div><br class=""></div><div>Ideally, you would use a protocol extension for the latter case. It would say that “all types which conform to StandardLibraryBitShiftable conform to _BitShiftable”. In this case, as we don’t have protocol-oriented Integers, and the current design makes this one particular case really annoying. We could probably have a shorthand syntax for this sort of case, too. I’m not worried about that at all; the important thing is that we get the semantics correct and that the model makes Swift types even more powerful. We can worry about reducing boilerplate later, because in theory it’s all about hooking up the correct protocol-witness table with the correct function pointer and we’ll have all the information to do that.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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 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;" class=""><div class=""></div><div class="">Basically, the way I see it, if my class MyClass implements MyProtocol, providing someRequiredFunc(), there’s an “ownership” chain there (reading it backwards).</div><div class=""><br class=""></div><div class="">Now what happens if MyClass implements MyOtherProtocol, which also has someRequiredFunc()? In that case, I want to MyClass as a MyOtherProtocol and get another function pointer, which just happens to have the same human-readable name as some other property. Just because they have the same function signature, absolutely doesn’t mean they’re the same thing.</div><div class=""><br class=""></div><div class="">Now, if we strongly bind all protocol conformances to the protocol they implement, what happens to instance methods? They don’t belong to any protocol, their parent is the class itself. If you have an instance method called someRequiredFunc(), and you later add a conformance to MyProtocol, you would need to declare that it belongs to MyProtocol. If you don’t want it to be an API-breaking change, you have to provide a thunk (or we could provide a shorthand syntax which emits thunks for you) to let us know that MyClass::someRequiredFunc() is the same thing as MyClass::MyProtocol::<wbr class="">someRequiredFunc().</div></div></blockquote><div class=""><br class=""></div><div class="">Your argument is that two methods with the same name should not in any way conflict with each other. This is a fundamental change from the status quo. If we were to take your argument to its logical conclusion, any member A of a type T should be capable of being designated as the implementation of a requirement B of protocol P. In the most general case, two functions A and B shouldn't even need to take the same number of arguments, or arguments of the same type; you should be able to supply default arguments, or even write custom code that takes arguments for A and computes suitable arguments for B in order to forward A to B, and the language should allow you to designate A as an implementation of B. But that is simply not how Swift protocols are designed.</div></div></div></div></blockquote><div><br class=""></div><div>Yes, that’s my argument. Changing the status quo is basically what swift-evo is _for_.</div><div><br class=""></div><div>And yes, you’ve basically got the concept - you should connect up the protocol requirement &lt;-&gt; implementation table by manually marking the protocol name on them (as the proposal says). Migration would be straight-forward.</div><div>But actually, I do think this fits with how Swift protocols are designed. In Objective-C, anything that responds to the same selectors as NSCopying conforms to NSCopying, because Objective-C’s dispatch works by selector names. In Swift, as you point out later, you need to manually declare that conformance even if it responds to the equivalent of “-(id) copyWithZone:”. A Swift protocol is a guarantee of a certain kind of behaviour, not just of functions with certain names.&nbsp;</div><div><br class=""></div><div>The status quo is that we are stuck in the middle - members are disambiguated only by their name, but just having the name isn’t enough for conformance. So when you come across a conflict in names, you need to rename all uses of one of them. If the protocol comes from a source you cannot change, you may be stuck in a situation where you *cannot conform to the protocol* and have to write your own wrapping thunk-er which does. Basically, you end up looking with code that looks like my proposal.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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 class="gmail_quote"><div class="">&nbsp;</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;" class=""><div class="">Let’s take an example where retroactive modelling could go wrong. You’ve got different teams working on different parts of an App, and they’ve all got their own convention for “copy()”. Sometimes it’s a deep-copy, sometimes a shallow-copy, sometimes it’s used in a fragile way for a specific case, whatever. Now you want to go and clean that up by creating a “Copyable” protocol with codified guarantees. Some objects may already conform, others may need tweaks, and some may want both behaviours simultaneously (preserving the old, non-Copytable-compliant behaviour until the next API break), depending on how you look at the object. A system like this allows all of those different ways of looking at the object live together. You could have the old, non-comforming API as an extension with a FIXME to delete it for version 2.</div></div></blockquote><div class=""><br class=""></div><div class="">Even if you design a protocol called Copyable, you still need to explicitly extend concrete types in order to conform to Copyable. Swift does not automagically make anything conform to your protocol. If you choose *explicitly* to conform different types that don't guarantee the same semantics, and then you erroneously assume that they all have the same semantics even though you *explicitly* chose types that don't have the same semantics, you're the one who shot yourself in the foot, so to speak. It's not the fault of Swift at all.</div><div class=""><br class=""></div></div></div></div></blockquote><div><br class=""></div><div>Yes, and the best bit about this is that you’d need to explicitly add it to every would-be member of Copyable too, so you’d have to check that the new semantics you are imposing with the conformance are correct (unless using some retroactive modelling shorthand which tells the compiler to emit thunks for all members of MyClass-&gt;Copyable in to MyClass). That’s the right thing to do, IMO (and also, would be the perfect cue for Xcode’s rename tool to start supporting Swift…).</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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 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;" class=""><div class=""><br class=""></div><div class="">I think it’s pretty arcane that members of a type are resolved only by their names. If you want to provide types which allow flexible views of data, each view of that data needs to be completely free in its expressivity.</div><div class=""><br class=""></div><div class="">I would actually like to see a syntax like:</div><div class=""><br class=""></div><div class="">```</div><div class="">let testObject = MyClass()</div><div class="">let testMyProto = testObject.MyProtocol // the protocol-witness table for testObject as a MyProtocol.</div><div class=""><br class=""></div><div class="">testObject.MyProtocol.<wbr class="">someRequiredFunc() // that’s one function</div><div class="">testObject.someRequiredFunc() // is a different function. May happen to have the same implementation as above if MyProtocol was retroactively modelled.</div><div class="">```</div><div class=""><br class=""></div><div class="">I think it would fit well with the dispatch system for protocol extensions, too. I sometimes have code like the following:</div><div class=""><br class=""></div><div class="">```</div><div class="">protocol Base {}</div><div class="">protocol Derived : Base {}</div><div class=""><br class=""></div><div class="">extension Base {&nbsp;</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>func doSomething() { … }</div><div class="">}</div><div class="">extension Derived {</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>func doSomething() {</div><div class="">&nbsp; &nbsp;…</div><div class="">&nbsp; &nbsp;(self as Base).doSomething() // Would be better if we could say “self.Base.doSomething()” to disambiguate instead of casting.</div><div class="">&nbsp;<span class="Apple-converted-space">&nbsp;</span>}</div><div class="">}</div><div class="">```</div></div></blockquote><div class=""><br class=""></div><div class="">This is a complete redesign of protocols in Swift. With the emphasis on minimizing source-breaking changes, I doubt such a change would be in scope for any phase of Swift unless you could show an overwhelming benefit.</div></div></div></div></blockquote><div><br class=""></div><div>Not really a complete redesign - it fits with the semantic model of protocols which has already been established. I expect that protocols are going to change a lot in Swift 4 anyway, what with conditional conformances and better existentials, they’re going to be way more powerful than Swift 3 protocols. It is important we unlock full expressivity to allow them to be fully exploited, so we have to tackle the naming-conflict problem.</div><div><br class=""></div><div>Also, I’m not sure source-breaking changes are such a big deal since we decided to support Swift 3 syntax. We could still support that - if you write “MyClass.someRequiredFunc()” which only exists inside a protocol, the compiler can search for the correct one to use.</div><div><br class=""></div><div>There are great ways we can extend this, too - if you’ve got the ability to disambiguate the protocol witnesses yourself, perhaps you could override the “MyProtocol” witness getter and return an optimised wrapper which conforms to the protocol on your behalf. That would mean that all of the various String views (UTF8/16/Scalars/Characters) could be implemented as protocols on the single String type. So now suddenly we have a standard protocol for “a collection of UTF code points” which String is able to conform to, so it’s much easier to write generic code works with String and my own custom data-type which is also representable as some UTF code points.</div></div><div><br class=""></div><div>I think that’s a pretty big win. It would allow us incredible new flexibility to model, which is ultimately what protocols are there to do.</div><div>It’s not even such a big change **at a high level** (I’m aware that, especially the first one, would touch just about every component of the compiler and standard library):</div><div><br class=""></div><div>- Protocol conformances own their members; Members which are part of a protocol conformance no longer belong to their enclosing type directly.</div><div>- Synthesise getter for “.ProtocolName”, which returns the protocol witness</div><div>- (Optional) Make overridable to supply your own protocol witness</div><div>- Insert jumps to allow for retroactive modelling, with shorthand syntax</div><div><br class=""></div><div>I’m not sure if it’s really a non-starter though. It would make the language much better and it would be pretty easy to migrate existing code (since we don’t allow non-unique names right now). Like I said, I expect the Swift 4 standard library (with conditional conformances) to look very different to Swift 3 at any rate.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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 class="gmail_quote"><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;"><div style="word-wrap: break-word;" class=""><div class=""></div><div class="">So yeah, a big +1 to marking protocol methods with their protocol (whatever the syntax ends up looking like), and actually I’d take it further and bake them in to the ABI. That also makes it relevant for Swift 4 phase 1.</div></div><div style="word-wrap: break-word;" class=""><div class=""><br class=""></div><div class="">Karl</div></div></blockquote></div></div></div></blockquote></div><br class=""></body></html>