Swift: Providing a default protocol implementation in a protocol extension

vrutberg

I'm trying to play around a bit with Swift, protocols and protocol extensions. Specifically I'm trying to provide a default implementation of a protocol in a protocol extension. This is my code:

protocol Proto : class {
    func someMethod() -> String
}

extension Proto {
    static func create() -> Self {
        return ProtoDefaultImpl() as! Self
    }
}

class ProtoDefaultImpl : Proto {
    func someMethod() -> String {
        return "doing something"
    }
}

let instance = Proto.create()
let output = instance.someMethod()

print(output)

The compiler complains on the line where I call Proto.create(), with the following error: error: static member 'create' cannot be used on instance of type 'Proto.Protocol'.

Have I missed something here? Is there any way you can achieve this?

Thanks.

Mr Beardsley

You can't call the method on the protocol itself, you have to call it on a type that implements the protocol. This doesn't change because there is a default implementation of the protocol in an extension. Change your type from Proto to ProtoDefaultImpl and it will work as you expect.

protocol Proto : class {
    func someMethod() -> String
}

extension Proto {
    static func create() -> Self {
        return ProtoDefaultImpl() as! Self
    }
}

class ProtoDefaultImpl : Proto {
    func someMethod() -> String {
        return "doing something"
    }
}

let instance = ProtoDefaultImpl.create()
let output = instance.someMethod()

print(output)

This outputs: doing something

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Default implementation of protocol extension in Swift not working

swift protocol extension default implementation vs actual implementation in class

override protocol extension default implementation

Swift - Default Implementation of protocol functions in another protocol

Swift - Protocol default implementation in extension with generic superclass constraint

Swift: Store Type of implementation of Protocol in static var of protocol extension

Using Swift protocol default implementation in Kotlin Multiplatform

Swift protocol default implementation for optional readonly variables

How the call the default implementation code in protocol in swift?

Extending a Protocol property to provide a default implementation in Swift

Swift: calling default implementation of protocol func from custom implementation

How to add default implementation using generic protocol extension?

How to override a protocol extension's default implementation of a func?

How to invoke protocol extension default implementation with type constraints

Swift : protocol extension and arrays

Dispatching to Swift protocol extension

Swift extension that conforms to protocol

ObjC protocol Implementation in Swift

swift protocol with default value

Swift: Is it possible to add a protocol extension to a protocol?

Swift protocol extension with property conforming to protocol

Swift: Why is the default protocol implementation used when an override is in place?

Swift property observer in protocol extension?

Swift 2: UITableViewDataSource protocol extension

Swift: Default value for properties in Protocol

Alternative to default argument in a protocol Swift

Swift overriding protocol extension keeping the extension behaviour

Function implementation in constrained protocol extension is not invoked

Swift protocol and extension, I need to call overridden method or default extension method as per requirement