How to stop redefining a method provided through extension

How to restrict struct from redefining a method for which a default implementation is available.

protocol SomeProtocol {
func someMethod()
}
extension SomeProtocol {
func someMethod() {
print("Default implementation is provided")
}
}
struct SomeStruct: SomeProtocol {
func someMethod() {
print("Implementation provided by struct")
}
}

Remove the method from the protocol (also called 'customization point' and 'protocol requirement') and just keep the protocol extension.

protocol SomeProtocol {}
extension SomeProtocol {
  func someMethod() {
    print("implementation is provided")
  }
}
struct SomeStruct: SomeProtocol {}
1 Like

No I'm looking for a keyword which provides the restriction, similar to "final" in class.

There is no such thing in Swift. I just showed you a way how it‘s done in Swift today. Why does it not fit your needs, is there any other place where you want to provide a custom implementation?

1 Like
Terms of Service

Privacy Policy

Cookie Policy