[swift-evolution] Keyword for protocol conformance

Xiaodi Wu xiaodi.wu at gmail.com
Mon Aug 22 17:19:00 CDT 2016


This has been proposed before in the past by several others (myself being
one of them).

The key problem is that it cannot accommodate retroactive modeling. That
is, you would not be able to conform existing types, the code for which you
do not control, to a protocol of your own design. Retroactive modeling is
an essential feature of Swift protocol-oriented programming.
On Mon, Aug 22, 2016 at 4:30 PM David Cordero via swift-evolution <
swift-evolution at swift.org> wrote:

>
> *The problem:*
> At the moment, looking at the code of a class or a struct implementing a
> protocol, it is hard to know what methods are actually implementing the
> protocol and what other methods are just part of the code of the class or
> struct.
>
> People are trying to fix this problem with pragma marks or moving the code
> conforming the protocol to a separate extension, but they are just specific
> good practices or code styles.
>
> *Proposal:*
> Adding a keyword to the methods conforming protocols. As an example please
> check the following piece of code which uses the keyword `conform` to
> explicitly indicate that `myMethod` is a method conforming a protocol.
>
> ```
> protocol MyProtocol {
>     func myMethod() -> String
> }
>
> class MyClass: MyProtocol {
>
>     *conform* func myMethod() -> String {
>         return "Yuhuuu,I am conforming \\o//"
>     }
>
>     func whatever() {
>         print("I am a boring method and I don't conform anything")
>     }
> }
> ```
>
> It would be something similar to the current keyword `override` but for
> protocols.
>
> Apart from improving code readability, It would allow the detection, in
> compilation time, of errors due to code evolution. For example redundant
> methods that no longer conform anything once the requirement is removed
> from the protocol for whatever reason.
>
> Looking forward your opinions and comments.
>
> Kind Regards
>
>
> _______________________________________________
> 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/20160822/5755fdf8/attachment.html>


More information about the swift-evolution mailing list