[swift-users] override-like keyword for default implementations

Slava Pestov spestov at apple.com
Tue May 16 17:05:30 CDT 2017


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



More information about the swift-users mailing list