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

Erica Sadun erica at ericasadun.com
Thu Apr 28 13:09:51 CDT 2016


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  { }
```

-- 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?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160428/b3d9af95/attachment.html>


More information about the swift-evolution mailing list