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

L. Mihalkovic laurent.mihalkovic at gmail.com
Sat Apr 30 02:33:05 CDT 2016



> On Apr 28, 2016, at 8:09 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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 : Frobnicate {
>     override required func frobnicate() { print("A") } // "A", or delete to get default
> }
> struct B: Frobnicate {
>     override required func frobnicate() { print("B") } // "B", or delete to get default
> }
> struct C: Frobnicate  { }
> ```

These are a couple of derived situations: 
> struct IAssumeThisIsAProblem : Frobnicate {
>     required func frobnicate() { print("A") } 
> }


> struct IAssumeThisIsAnotherProblem  Frobnicate {
>     override func frobnicate() { print("A") } 
> }


What happens in both cases?

> 
> -- E
> 
> 
>> On Apr 28, 2016, at 11:48 AM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>> 
>> 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/20160430/b848b358/attachment.html>


More information about the swift-evolution mailing list