[swift-evolution] [Pitch] Requiring proactive overrides for default protocol implementations.
Xiaodi Wu
xiaodi.wu at gmail.com
Thu Apr 28 13:26:22 CDT 2016
Right, sorry, I should have clarified. The scenario to be supported here is
that the code for structs A, B, and C cannot be modified. They could be
part of the stdlib, for example, or a closed-source third-party library.
On Thu, Apr 28, 2016 at 1:18 PM, Matthew Johnson <matthew at anandabits.com>
wrote:
>
>
> Sent from my iPad
>
> On Apr 28, 2016, at 1: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 { }
> ```
>
> We can't add the keywords if the structs are defined in a module we import
> but don't own. We are only declaring the conformance retroactively. The
> ability to do this is a crucial aspect of generic programming. It isn't
> yet clear how your proposal handles retroactive modeling.
>
> -- 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/20160428/5630611d/attachment.html>
More information about the swift-evolution
mailing list