deepika
(deepika ramesh)
November 12, 2019, 10:33am
1
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
deepika
(deepika ramesh)
November 12, 2019, 10:41am
4
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