[swift-evolution] [Pitch] Requiring proactive overrides for default protocol implementations.

Matthew Johnson matthew at anandabits.com
Thu Apr 28 13:04:14 CDT 2016


I'm glad you brought this up and provided a clear example Xiaodi.

Erica, as I mentioned a few days ago (and Dave A gave +1) this proposal cannot break retroactive modeling if it is going to fly.  Can you please update it to clarify how you envision it interacts with retroactive modeling?  Would these new keywords only be required when the conformance is declared in the same module as the method? 

Sent from my iPad

> On Apr 28, 2016, at 12:48 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> On Thu, Apr 28, 2016 at 12:25 PM, Erica Sadun <erica at ericasadun.com> wrote:
>> >
>> > On Apr 28, 2016, at 11:05 AM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>> >
>> > How is retroactive modeling accommodated in this scheme? Say I want to conform three types I don't own to a protocol of my design and supply a default implementation for a protocol requirement. How would I go about it?
>> 
>> extension NotMyType: ProtocolIdesigned {...}
>> 
>> extension NotMyProtocol {
>>      required func method1() {...}
>>      override required func method2() {...} // assumes NMP has already provided default
>>      func newThing() {...}
>> }
>> 
>> The only tricky bit is when NotMyProtocol already has a default required func implementation and an override required func implementation (although that should be rare). In such case, you probably have to create a new protocol DerivedProtocol: NotMyProtocol and work from there. The override in the DerivedProtocol will then take priority over the override in NotMyProtocol.
> 
> Sorry, that's not my question, which doesn't involve protocols I don't own. Let me restate. Given three types I don't own as follows:
> 
> ```
> struct A {
>     func frobnicate() { print("A") }
> }
> struct B {
>     func frobnicate() { print("B") }
> }
> struct C { }
> ```
> 
> I want to conform them to a protocol of my own design, Frobnicatable, and supply a default `frobnicate()`:
> 
> ```
> protocol Frobnicatable {
>     func frobnicate()
> }
> extension Frobnicatable {
>     func frobnicate() { print("Default") }
> }
> extension A: Frobnicatable { }
> extension B: Frobnicatable { }
> extension C: Frobnicatable { }
> 
> let c = C()
> c.frobnicate() // "Default"
> ```
> 
> (Yes, I realize there are issues regarding static and dynamic dispatch that limit the utility of this particular example--let's leave those aside for now.)
> Where would I affix keywords such as `required` and `override` to make this work after implementation of your proposal?
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160428/e99292dd/attachment.html>


More information about the swift-evolution mailing list