In my code I have a generic class with several subclasses. What I would like to do is to check if an object is of one of those generic subclasses. Take this code:
class Container<T> {}
class Object {}
class SubObject: Object {}
let container1 = Container<Object>()
let container2 = Container<SubObject>()
let array1 = Array<Object>()
let array2 = Array<SubObject>()
print(container1 as? Container<Object>) // test 1: Conditional cast from 'Container<Object>' to 'Container<Object>' always succeeds
print(container2 as? Container<Object>) // test 2: Cast from 'Container<SubObject>' to unrelated type 'Container<Object>' always fails
print(array1 as? Array<Object>) // test 3: Conditional cast from 'Container<Object>' to 'Container<Object>' always succeeds
print(array2 as? Array<Object>) // test 4: Conditional cast from 'Container<Object>' to 'Container<Object>' always succeeds
For the boolean tests 1, 3, and 4, the compiler generates the warning Conditional cast from 'Container<Object>' to 'Container<Object>' always succeeds, while for test 2 the warning reads Cast from 'Container<SubObject>' to unrelated type 'Container<Object>' always fails. I don't understand why. Is there a way of changing my generic class so that test 2 returns true?