Extension of protocol with method which adds default parameter

Vladyslav Zavalykhatko

I'm used to having default parameters inside protocols using extensions, as protocol declarations themselves can't have them, like this:

protocol Controller {
   func fetch(forPredicate predicate: NSPredicate?)
}

extension Controller {
   func fetch(forPredicate predicate: NSPredicate? = nil) {
      return fetch(forPredicate: nil)
   }
}

Worked perfectly for me.

Now I have the next situation, I have one specific protocol for specific kind of controller:

protocol SomeSpecificDatabaseControllerProtocol {
    //...
    func count(forPredicate predicate: NSPredicate?) -> Int
}

And protocol-extension with implementations of default methods for controllers:

protocol DatabaseControllerProtocol {
    associatedtype Entity: NSManagedObject
    func defaultFetchRequest() -> NSFetchRequest<Entity>
    var context: NSManagedObjectContext { get }
}

extension DatabaseControllerProtocol {
    func save() {
        ...
    }

    func get() -> [Entity] {
        ...
    }

    func count(forPredicate predicate: NSPredicate?) -> Int {
        ...
    }

    //.....
}

My issue is when I'm trying to add method with default parameter to the SomeSpecificDatabaseControllerProtocol extension, I'm receiving a compile-time error, that concrete class conforming to SomeSpecificDatabaseControllerProtocol doesn't conform to the protocol (happens only if I extend protocol):

class SomeClassDatabaseController: SomeSpecificDatabaseControllerProtocol, DatabaseControllerProtocol {...}

What am I missing?

Martin

This is happening because compiler is confuse due to ambiguous functions.

  1. Here SomeClassDatabaseController receiving count() method from two different protocols.

  2. DatabaseControllerProtocol has count(forPredicate) method which always need parameter.

  3. On other hand SomeSpecificDatabaseControllerProtocol have count() method which can have empty parameter.

  4. To solve this either you have to change count method in DatabaseControllerProtocol to this or you have to implement it in SomeClassDatabaseController.

func count(forPredicate predicate: NSPredicate? = nil) -> Int { return 0}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Which extension adds jquery?

Protocol extension default method causing a "self is immutable" error

"Type of a class which conforms to a protocol" as parameter in a method swift

How to call a swift method which parameter is a protocol from Objective C

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

Error while using default value in parameter of Protocol's method

Can't create default closure parameter in Array extension method in Swift

override protocol extension default implementation

Swift: Providing a default protocol implementation in a protocol extension

Swift 4 extension function on generic protocol which takes a generic function as parameter

Which Extension Method is called

Always the default implementation of protocol gets called even after implementing the method in class extension in an XCTest file

Implementing a default equals method for a protocol

How to force usage of protocol default extension "override"?

Default implementation of protocol extension in Swift not working

Is "this" a default parameter in a class method?

Swift function with a parameter which conforms to a protocol

Protocol extension method dispatch in Swift 2.0

Swift protocol extension method dispatch with superclass and subclass

Implementing a function with a default parameter defined in a protocol

Extension method resolution by parameter names

Generic extension method parameter restrictions

Calling protocol default implementation from regular method

Cast to constrained protocol can not access default method

swift method accept parameter if conform a protocol

Protocol method does not pass the correct parameter

Is it okay use NSDictionary as a parameter in protocol method?

Passing a COM method default parameter

Select a method as (default) template parameter