[swift evolution] [proposal] Proposal to add "implement" keyword to denote protocol method implementation


(Victor Gao) #1

From: Victor Gao <victorgao44@gmail.com>
Subject: Re: [swift-evolution] [swift evolution] [proposal] Proposal to add "implement" keyword to denote protocol method implementation
Date: March 18, 2016 at 21:48:53 ADT
To: Ross O'Brien <narrativium+swift@gmail.com>

Hi Ross,

That’s a good point actually, your point about retroactive modeling.

Correct me if I’m wrong, but what you are saying is that if you have a class that has some methods, and then you decided to create a protocol that has the methods of the class. If you want the class to conform to the protocol, you want to be able to do so without having to add the “implement” keyword in front of every method implementation. Is that right?

I also understand that one of the problems with the “implement” keyword: when you create an extension that adds a protocol conformance to a class, if that the class already has the methods defined in the protocol, it wouldn’t work because the methods in the class don’t have the “implement” keyword! This is a perplexing problem.

Victor Gao

I recently had the misfortune of deleting what turned out to be a necessary function, because it was an optional requirement of a protocol and wasn't being called by any code in the project, so I would sympathise with this.

On the other hand I've also written protocols composed entirely of functions that an established type already has, so I can retroactively have the type conform. (For example, there's no 'ArithmeticOperation' protocol which Int and Double both conform to, even though they both have a `+ (lhs:Self, rhs:Self) -> Self` operator, so you have to roll your own... unless that should be a proposal itself.) It would be impossible to require a function to use your 'implements' keyword to conform to a protocol in an importing module.

···

Begin forwarded message:

On Mar 18, 2016, at 21:11, Ross O'Brien <narrativium+swift@gmail.com> wrote:


(Ross O'Brien) #2

At the moment I'm considering Jordan's point: "(IIRC last time we got to
"it's required if the member is in the same module, but allowed to be
absent otherwise", which is fairly reasonable but probably still needs to
be thought through.)"

Taking my example: Int, Int16, Int32, Int64 etc. conform to
IntegerArithmeticType, so they have the operator `func + (lhs:Self,
rhs:Self) -> Self`. Double and Float have this function but there's no
shared protocol, so I write my own `Summable` protocol and extend the
existing types to conform to it.

Is there a need for a counterpart keyword to 'implements': 'retroactive'? I
don't think so; I could declare my protocol with 'retroactive protocol
Summable' and then write my own custom Decimal type which conforms to
Summable (and then uses 'implements func + (lhs: Decimal, rhs: Decimal) ->
Decimal') and I don't know if I've gained anything from it.

I think perhaps '@implements' could be a useful attribute? Not actually
required by the compiler, but a useful signifier while reading code. And
if, during the course of a project's development, a function is removed
from a protocol, the compiler could warn that an @implements function isn't
actually required by any protocol the implementing type claims to conform
to.

···

On Sat, Mar 19, 2016 at 1:14 AM, Victor Gao via swift-evolution < swift-evolution@swift.org> wrote:

Begin forwarded message:

*From: *Victor Gao <victorgao44@gmail.com>
*Subject: **Re: [swift-evolution] [swift evolution] [proposal] Proposal
to add "implement" keyword to denote protocol method implementation*
*Date: *March 18, 2016 at 21:48:53 ADT
*To: *Ross O'Brien <narrativium+swift@gmail.com>

Hi Ross,

That’s a good point actually, your point about retroactive modeling.

Correct me if I’m wrong, but what you are saying is that if you have a
class that has some methods, and then you decided to create a protocol that
has the methods of the class. If you want the class to conform to the
protocol, you want to be able to do so without having to add the
“implement” keyword in front of every method implementation. Is that right?

I also understand that one of the problems with the “implement” keyword:
when you create an extension that adds a protocol conformance to a class,
if that the class already has the methods defined in the protocol, it
wouldn’t work because the methods in the class don’t have the “implement”
keyword! This is a perplexing problem.

Victor Gao

On Mar 18, 2016, at 21:11, Ross O'Brien <narrativium+swift@gmail.com> > wrote:

I recently had the misfortune of deleting what turned out to be a
necessary function, because it was an optional requirement of a protocol
and wasn't being called by any code in the project, so I would sympathise
with this.

On the other hand I've also written protocols composed entirely of
functions that an established type already has, so I can retroactively have
the type conform. (For example, there's no 'ArithmeticOperation' protocol
which Int and Double both conform to, even though they both have a `+
(lhs:Self, rhs:Self) -> Self` operator, so you have to roll your own...
unless that should be a proposal itself.) It would be impossible to require
a function to use your 'implements' keyword to conform to a protocol in an
importing module.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution