So I've got this class, and it's generic with respect to types that conform to some protocol.
However, I want to add some specific functionality if the type happens to conform to a specific sub-protocol.
So I've done that with an extension, like this:
protocol A
{
func aFunction()
}
protocol B : A
{
func bFunction()
}
class MyClass<T:A>
{
var myVariable : T? = nil
func doSomethingWithA()
{
myVariable?.aFunction()
}
}
extension MyClass where T:B
{
func doSomethingWithB()
{
myVariable?.bFunction()
}
}
enum TestType : B
{
case Someting
func aFunction() { print("A function") }
func bFunction() { print("B function") }
}
let testObj = MyClass<TestType>()
testObj.myVariable = TestType.Someting
testObj.doSomethingWithB()The problem is that I can't call the specific method outside of its extension. For example, I can't call doSomethingWithB() from doSomethingWithA().
I don't expect to be able to just call it like that, because doSomethingWithB won't exist if T doesn't conform to B. But I can't find any way to call it.
I suppose I'm looking for some way to locally narrow down the scope of T with a generic constraints list. I've tried to call an overloaded generic function (one overload for T:A and one for T:B), but if I call it from doSomethingWithA, the T:A version is called even though a more specific version is available. Is anything like this possible with Swift 2?
The reason why I'm doing this is because I want these specialised methods to hook in to the base implementation's loading cycle. I need to use generic constraints rather than declaring variables and parameters in terms of protocols because in my production code protocol A has an associated type and B uses Self.