I'm not sure I understand. What compiler or language support is missing for StringCore?<br><div class="gmail_quote"><div dir="ltr">On Tue, Sep 20, 2016 at 16:42 Karl via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On 20 Sep 2016, at 23:28, Karl <<a href="mailto:raziel.im+swift-evo@gmail.com" target="_blank">raziel.im+swift-evo@gmail.com</a>> wrote:</div><br><div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On 20 Sep 2016, at 18:43, Nevin Brackett-Rozinsky via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div dir="ltr">I have been following this discussion (as well as similar threads earlier this year) and listening to the ideas put forth by all sides.<div><br></div><div>It seems to me that the fundamental difference between classes and protocols is that classes inherit implementation whereas protocol conformance is a promise about interface.</div><div><br></div><div>When a class or struct or enum declares itself as conforming to a protocol, that means it has all the members specified in the protocol. The protocol conformance simply codifies a fact about the type itself: namely that all those members are present.</div><div><br></div><div>In this model, any keyword such as `implements` on each conforming member would introduce substantial boilerplate for negligible gain. The purpose of a protocol is to communicate that certain members are available, not to make declaring those members more onerous.</div><div><br></div><div>However, default implementations for protocols blur the line. Now there is actual implementation being inherited. A conforming type may choose to roll its own version of a method, or to utilize the default provided by the protocol. This is closer to the situation with subclassing.<br></div><div><br></div><div>Moreover, a protocol which conforms to another protocol may itself define (or redefine!) default implementations for members of that other protocol. This can create “inheritance chains” of protocol default implementations. I think there is value in being able to refer to (and call) the inherited default implementation through some sort of `super` functionality.</div><div><br></div><div>On the other hand, the existence of a default implementation in a protocol is in large part merely a convenience: a courtesy so that each conforming type need not rewrite the same boilerplate code.</div><div><br></div><div>A type which conforms to a protocol may accept the default or it may provide its own implementation, but it is not “overriding” anything. The default implementation was offered as a convenience, to be taken or left as needed. Thus I do not think any keyword (neither `override` nor `implements`) should be required in that case either.</div><div><br></div><div>The frequently-raised point regarding near-miss member names deserves some attention. Several people have expressed a desire for the compiler to assist them in determining whether a given member does or does not meet a protocol requirement. Specifically, when a type conforms to a protocol with a default implementation, and the type defines a member with a similar signature, it is not obvious at glance if that member matches the protocol.</div><div><br></div><div>I think this is a job for linters and IDEs. For example, syntax highlighting could distinguish members which satisfy a protocol requirement, thereby providing immediate visual confirmation of success.</div><div><br></div><div>Having followed the lengthy discussion and weighed the numerous ideas put forth, I come down firmly on the side of no keyword for protocol conformance.</div><div><br></div><div>A protocol describes an interface and provides a set of customization points. It may also, as a convenience, offer default implementations. The protocol simply describes the capabilities of its conforming types, and any default implementations are there to make things easier for them.</div><div><br></div><div>Conforming types should not be afflicted with extraneous keywords: that would run contrary to the purpose of having protocols in the first place.<br></div><div><br></div><div>Nevin</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 20, 2016 at 11:16 AM, Xiaodi Wu via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">As I mentioned above, I agree that better diagnostics for near-misses are necessary, but they are possible without new syntax. There is no win in avoiding unintentional behavior because, without a default implementation, these issues are caught at compile time already.<div><div><br><div class="gmail_quote"><div dir="ltr">On Tue, Sep 20, 2016 at 10:14 Vladimir.S via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
> extension P {<br>
> implement func foo() -> [String : String] { return [:] }<br>
> }<br>
<br>
Yes, it seems like we need `implement` (or `override` as another<br>
suggestion) in protocol extension also just for the same reasons - be clear<br>
about our intention regarding implementing the requirement, to show that<br>
this func *depends* on the previous definition of P protocol and to avoid<br>
possible mistakes related to protocol conformance.<br>
<br>
On 20.09.2016 17:38, Charles Srstka wrote:<br>
>> On Sep 20, 2016, at 8:17 AM, Vladimir.S via swift-evolution<br>
>> <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>> wrote:<br>
>><br>
>> On 20.09.2016 3:03, Xiaodi Wu via swift-evolution wrote:<br>
>>> I definitely think Vladimir's suggestion is a great starting point, IMO.<br>
>>><br>
>>> However, I think it could be improved in one key respect where previous<br>
>>> proposals using `override` are superior. Namely, the proposed `implement`<br>
>>> keyword adds no additional safety when a type implements a protocol<br>
>>> requirement that doesn't have a default implementation. This is because, if<br>
>><br>
>> Yes, *at the moment of writing* the type's code there could be no default<br>
>> implementation for protocol requirement. But, *at the moment of<br>
>> compilation* such default implementation could appear.<br>
>><br>
>> Let's discuss such scenario in case we'll take your suggestion:<br>
>><br>
>> You got SomeClass.swift file, 3rd party file you don't want to change or<br>
>> changes are not allowed. Content:<br>
>><br>
>> public protocol SomeProtocol {<br>
>> func foo()<br>
>> }<br>
>><br>
>> public class SomeClass : SomeProtocol {<br>
>> func foo() {...} // no default implementation *at the moment of writing*,<br>
>> no need in `overload`<br>
>> }<br>
>><br>
>> Now, you adds SomeClass.swift file to your project and in some *other*<br>
>> file you write:<br>
>><br>
>> extension SomeProtocol {<br>
>> func foo() {...}<br>
>> }<br>
>><br>
>> As you see, you don't control the SomeClass.swift but you suggest in this<br>
>> case SomeClass.foo() should be defined with `override`.<br>
>><br>
>> With 'implement' SomeClass.foo() will be marked initially and will save<br>
>> us if protocol's requirement PLUS default implementation changed.<br>
><br>
> Requiring the ‘implement’ keyword can help us even if no default<br>
> implementation is involved. Consider:<br>
><br>
> protocol P {<br>
> func foo() -> [String : Any]<br>
> }<br>
><br>
> struct S : P {<br>
> func foo() -> [String : String] { return [:] }<br>
> }<br>
><br>
> We will get an error here that S does not conform to P. However, this is<br>
> not the correct error, since S in fact *tries* to conform to P, but it has<br>
> a mistake in a method signature. This misleads us as to the true nature of<br>
> the problem, and if S has enough members in it that we fail to spot the<br>
> existing foo(), we might solve the problem by reimplementing foo(), and<br>
> leaving the original foo() as dangling dead code. Having an ‘implement’<br>
> keyword on the existing foo() function would change the compiler error to<br>
> let us know that we have an existing foo() that is incorrectly declared.<br>
><br>
> In addition, ‘implement’ can help us when the declaration in question *is*<br>
> the default implementation:<br>
><br>
> protocol P {<br>
> func foo() -> [String : Any]<br>
> }<br>
><br>
> extension P {<br>
> implement func foo() -> [String : String] { return [:] }<br>
> }<br>
><br>
> Here we will get an error with the proposed ‘implement’ keyword, because<br>
> foo() does not have a signature matching anything in the protocol, whereas<br>
> without ‘implement’ we would happily and silently generate a useless<br>
> dangling function that would never be used, and then pass the buck to the<br>
> concrete type that implements P:<br>
><br>
> protocol P {<br>
> func foo() -> [String : Any]<br>
> }<br>
><br>
> extension P {<br>
> func foo() -> [String : String] { return [:] } // The error is here:<br>
> }<br>
><br>
> struct S : P {} // But it gets reported here.<br>
><br>
> Charles<br>
><br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
</blockquote></div>
</div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
<br></blockquote></div><br></div>
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote><br></div><div><br></div><div>I agree that a new keyword is unwanted. Conforming to protocols is quite a common thing, so you want it to be easy to remember.</div><div><br></div><div>I think the best way is to prefix the member name with the protocol, e.g:</div><div><br></div><div>protocol MyProto {</div><div> var aVariable : Int</div><div> func aFunction()</div><div>}</div><div>class MyClass : MyProto {</div><div> var MyProto.aVariable : Int</div><div> func MyProto.aFunction() { … }</div><div>}</div><div><br></div><div>This is consistent with how we refer to other members of types (e.g. “extension MyClass.MyInternalClass”). It will be easy for autocompletion to provide good suggestions, too.</div><div>As I see it, the only problem is what if `MyClass` wants its own function called `aFunction()`? What if the same name satisfies 2 protocols, which do you write?</div><div><br></div><div>The way to solve all of the problems in a consistent way is to make the function actually called “MyProto.aFunction”, and for it to be a separate function from plain “aFunction()” or from “SomeotherProto.aFunction”.</div><div><br></div><div>I believe it is crucial to protocols that we can do this. Maybe I have some complex data structure and it has its own API, but I want people to be able to view it as a Collection. By conforming to Collection, I reserve lots of keywords and indexing operations which I now can’t use in my own API. Maybe I’m just providing Collection as a convenience to work with generic algorithms, but my own API has more efficient semantics for some operations. We’re relegated to using less-obvious and legible names in order to avoid conflicts.</div><div><br></div><div>We have a way to work around this, which String uses - create a struct which references your object and calls internal methods such as “_collection_count” so you can have separate interfaces. This adds up to quite a lot of boilerplate and maintenance overhead.</div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><div>Also to add here: you’re basically implementing what I’m proposing manually if you do this; only you don’t get language/compiler support.</div><div>String basically does this - it shares StringCore with UTF8View and defines some internal functions to support it.</div><div><br></div><div>The String views could then be made in to protocols on String, turning “UTF8View” in to “UTF8Representable”, and opening up algorithms which can work on generic sequences of UTF8 bytes. I think that’s pretty cool, and could open up better integration with other types which are (for example) UTF8Representable — for example a stream of UTF8 bytes (depending on how flexible implementation allows us to make the protocol).</div></div></div><div style="word-wrap:break-word"><div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div><br></div><div>I don’t agree that Protocol conformances are kind-of incidental, as others here have written. This isn’t like Objective-C where anything that has the correctly-named methods conforms. Protocol conformances are completely explicit, and in fact we have empty protocols (“marker protocols”) for exactly that purpose. I think it is consistent that we make every member of a conformance specify which protocol it belongs to, and to have its name scoped to that protocol.</div><div><br></div><div>Karl</div><br><div><br></div><div><div>CC-ing Dave A, to understand better if this fits with the vision of protocols</div></div><div><br></div></div></div></blockquote></div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
</blockquote></div>