[swift-evolution] [Pitch] Requiring proactive overrides for default protocol implementations.
Xiaodi Wu
xiaodi.wu at gmail.com
Thu Apr 28 12:48:54 CDT 2016
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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160428/6c421d2b/attachment.html>
More information about the swift-evolution
mailing list