0
votes

I have a protocol that declares property of type Int. I also have few classes that conforms that Protocol, and now I need to overload operator + for all of them. Since operator + will work based on the property declared, I don't want to implement that operator in each class separately.

So I have

protocol MyProtocol {
    var property: Int { get }
}

And I would want to have something like

extension MyProtocol {
    static func +(left: MyProtocol, right: MyProtocol) -> MyProtocol {
        // create and apply operations and return result
    }
}

And actually I successfully did that, but trying to work with it I get an error ambiguous reference to member '+'.

When I move operator overload func to the each class separately, issue disappeared, but I'm still looking for a solution to make it works with Protocols.

1

1 Answers

3
votes

Solved, by moving func +... outside of Extension, so it is just a method in the file where MyProtocol is declared

protocol MyProtocol {
    var property: Int { get }
}

func +(left: MyProtocol, right: MyProtocol) -> MyProtocol {
    // create and apply operations and return result
}