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

Xiaodi Wu xiaodi.wu at gmail.com
Thu Apr 28 21:46:01 CDT 2016


On Thu, Apr 28, 2016 at 9:40 PM, Erica Sadun <erica at ericasadun.com> wrote:
>
> Without actually trying to understand the details of your math stuff:
>
> * If you add a required member in a declaration or extension that declares
> conformance, it is 'required'.
> * If it is already defaulted, it is `override required`.
> * If it is already defaulted but not required, it is `override`
> * If someone else implements the stuff, you still have to pull it in
> somehow, but if you do so by conforming to another protocol with an
> extension, it's not your business, so you don't use any keywords.
>
> You use keywords only for stuff that you specifically write, that
> clarifies the context in which you are writing it. If you do not own a
> protocol, an extension, or an implementation, you do not change or markup
> the protocol, extension, or implementation. You're just offering the
> compiler hints that your otherwise questionable decisions are fully
> intentional: when overriding an existing implementation and when conforming
> by supplying a required member.
>
> -- E, who still probably missed your point and again apologizes
>
>
> * If this is not Swift code it is not affected.
> * If it is Swift code, either use #if swift(>= blah) workarounds or
> propose that SwiftPM support earlier Swift compilation rules.
> * If this is adopted and the code is in Swift 3, it would already have
> compliances and you do not need to add anything
>
> Under what scenario could you possibly use 3rd party Swift 3 code
> (assuming adoption) that would require annotation/changing?
>

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160428/3e24c532/attachment.html>


More information about the swift-evolution mailing list