[swift-users] override-like keyword for default implementations
Howard Lovatt
howard.lovatt at gmail.com
Tue May 16 22:57:54 CDT 2017
In the proposal I just posted in reply to Jordan Rose all these examples
would work as expected, though the syntax and/or semantics would be
slightly different:
Example 1
In R.swift
protocol R { func f() } // f is abstract
In P.swift
protocol P {} // f cannot be in protocol and extension
extension P { func f() {... } } // f is implemented and declared to be in P
In S.swift
struct S: R, P {} // f is from P whether an instance of S is types as S, P,
or R
// If there was a spelling mistake for f in
either P or R it would be detected since not all methods would be
implemented
Example 2
In P.swift
protocol P {} // f cannot be in protocol and extension
extension P where Self: AnyObject { func f() {... } } // f is implemented
and declared to be in P if Self is an Object, otherwise abstract in P
In C:.swift
class C: P {} // Picks up f's implementation since Self is an object
Would this be an acceptable solution - it is minorly source breaking, but I
feel all solutions will be.
-- Howard.
On 17 May 2017 at 08:05, Slava Pestov via swift-users <swift-users at swift.org
> wrote:
> It’s not clear if such a keyword can be designed in a way that covers all
> cases. For example, consider a protocol extension that provides a default
> implementation for a requirement in a different protocol:
>
> protocol R {
> func f()
> }
>
> protocol P {}
>
> extension P {
> func f()
> }
>
> struct S : R, P {}
>
> Or a constrained extension that provides a default that only applies in
> certain cases:
>
> protocol P {
> func f()
> }
>
> extension P where Self : AnyObject {
> func f()
> }
>
> class C : P {}
>
> Slava
>
> > On May 16, 2017, at 8:53 AM, Johannes Weiss via swift-users <
> swift-users at swift.org> wrote:
> >
> > Hi swift-users,
> >
> > Is there anything like the `override` keyword for making sure that
> default implementations in protocols are not adding a new function?
> >
> > An example would be the following:
> >
> > protocol FooProto {
> > func foo()
> > }
> >
> > extension FooProto {
> > func foo() { /* <-- can I mark this as being a default
> implementation */
> > print("foo default")
> > }
> > }
> >
> > Right now, there's a `func foo()` default implementation for `FooProto`
> but if later of `foo` gets refactored to `bar`, we lose the default
> implementation which can lead to problems.
> >
> > Is there anything in Swift (like the `override` keyword) that allows me
> to say this is a default implementation and not a new method?
> >
> > Thanks,
> > Johannes
> > _______________________________________________
> > swift-users mailing list
> > swift-users at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-users
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170517/9131df91/attachment.html>
More information about the swift-users
mailing list