2

Is there a way to define a generic function in a protocol and allow the conforming object to define specializations for that protocol? For example:

protocol Generic {
    func generic<T>(prop: T, otherProp: String)
}

class Gen: Generic {
    func generic<T>(prop: T, otherProp: String) {
        print("generic")
    }

    func generic(prop: String, otherProp: String) {
        print(prop)
    }
}

Now if I use the class like so:

let inst = Gen()
inst.generic(prop: 1, otherProp: "Go")
inst.generic(prop: "Hello", otherProp: "Stop")

I get the expected result of:

generic
Hello

However if I declare inst to be of type Generic:

let inst: Generic = Gen()
inst.generic(prop: 1, otherProp: "Go")
inst.generic(prop: "Hello", otherProp: "Stop")

I get:

generic
generic

So, if I have a property of type Generic, I am unable to use the specialization of the generic function from the implementor of the protocol. Is this expected behavior? Is there a way to achieve the behavior I am looking for, i.e. to use the specialization of the generic function even when accessed through the protocol's interface? I'd appreciate any insight into this. Thanks everyone.

1
  • Phelippe has the answer, but I would be extremely careful with this. It should only be used when you can provide a performance improvement. It these implementations have different behaviors, this is a very fragile and difficult to debug, and the rules can be quite subtle (such as if you call a different generic method that then calls this, you may lose the specialization). Basically, if you have f<T>(value: T) and f(value: String), they should have the same visible behavior for String; the latter just might be more efficient.
    – Rob Napier
    Commented Mar 14, 2019 at 14:01

2 Answers 2

2

If you declare the protocol requirement to be a generic function, you cannot call a more specialised overloaded version of the same function via the protocol type. However, you can specialise the implementation of the generic function for your adopting class by checking the type of the generic input argument.

class Gen: Generic {
    func generic<T>(prop: T, otherProp: String) {
        if prop is String {
            print(prop)
        } else {
            print("generic")
        }
    }
}

let inst: Generic = Gen()
inst.generic(prop: 1, otherProp: "Go")
inst.generic(prop: "Hello", otherProp: "Stop")
2
  • In the end this is what I ended up doing. I guess I was hoping I missed something or that someone could tell me definitely that is the expected behavior. I think I may try to create an issue in the swift project and see if the functionality I’m looking for can be added. Thanks for your answer, though.
    – dudeman
    Commented Mar 14, 2019 at 14:48
  • @dudeman even if it was technically possible, I don't think this functionality will or should be implemented. Overloading a protocol requirement in a conforming class shouldn't result in that overloaded version being called when accessed through the protocol type, since the overloaded version isn't declared on the protocol itself. If you case inst to Gen in your example, then you can call the overloaded version directly. Commented Mar 14, 2019 at 14:56
0

You could add the method signature generic(String, String) in the protocol and add a default implementation with an extension:

protocol Generic {
    func generic<T>(prop: T, otherProp: String)
    func generic(prop: String, otherProp: String)
}

extension Generic {
    func generic(prop: String, otherProp: String) {
        generic(prop: prop as Any, otherProp: otherProp)
    }
}
1
  • I considered a solution similar to this but the big issue I see with this solution is in order for me to add a specialization to a type that conforms to the protocol, I must modify the protocol. If I don’t have the ability to modify the protocol, this won’t work. And, in general, I’d say it’s bad practice to modify an interface to fit a specific implementation. Thank you for the suggestion, though.
    – dudeman
    Commented Mar 14, 2019 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.