[swift-evolution] [Pitch] Requiring proactive overrides for default protocol implementations.
Xiaodi Wu
xiaodi.wu at gmail.com
Thu Apr 28 22:26:03 CDT 2016
On Thu, Apr 28, 2016 at 9:53 PM, Erica Sadun <erica at ericasadun.com> wrote:
>
> On Apr 28, 2016, at 8:46 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> Let's return to the toy example. Suppose I license the following code from
> a third party. I am allowed to incorporate it unmodified into my project:
>
> ```
> // I cannot touch any of the following code
> struct A {
> func frobnicate() { print("A") }
> }
> struct B {
> func frobnicate() { print("B") }
> }
> struct C { }
> ```
>
> The code above has three types that conform to no protocols. Nothing would
> change on adoption of your proposal. As licensed to me from the third
> party, there are no protocols for it to conform to.
>
> Now, in a separate file, as part of my own code, I want to conform these
> three types 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 a = A()
> a.frobnicate() // "A"
> let c = C()
> c.frobnicate() // "Default"
> ```
>
> It seems like there is nothing I can do to make this work upon
> implementation of your proposal.
>
>
> Is this your desired behavior or is this the behavior you expect?
> By implementing a default, you inherit that behavior in all three, because
> *you* added it.
> I expect the compiler to complain at your default because you did not mark
> it as required.
>
> If you want to use the inherent frobnicate and not the default one that
> you just added,
> you will need to do something like:
>
> extension A: Frobnicate {
> override required frobnicate = A.frobnicate
> }
>
What's shown in comments is how it *currently* behaves. That can be
verified in a playground (or the IBM sandbox online). I'm not sure I have a
strong opinion yet on how it *should* behave, although what I'm asking is
whether I *can* make it behave in the same way as it does currently.
If what you show above as a solution is part of your proposal, then that
should be sufficient to support my use case. Please do document it in the
text though. IMO, this is key. It's not enough to say, just modify the
original code, or just use a compiled module, or hope that the code is
written in something other than Swift 3.
I still have reservations about requiring both `override` and `required`,
which current Swift syntax never does. I understand what you're getting at,
but I'm not sure making the distinction between `override` and `override
required` will provide additional safety in practice. My reasoning:
`override` implies that there's an implementation provided somewhere, and
`required` implies there's a protocol requirement somewhere, but either one
alone is sufficient to tell the compiler "yes, I intend to write a function
with the same name as something else--please tell me if I failed to do so."
What would be lost (safety-wise) if we followed Swift precedent, and
`override required` got simplified to `override`?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160428/90a67520/attachment.html>
More information about the swift-evolution
mailing list