Short answer: you cannot distinguish Box<String> to Box<Any> because there is no relationship between them.
This page is for Java, but it also applies here.
Given the two specific types A and B (for example, a number and an integer), MyClass <A> is not related to MyClass <B>, regardless of whether A and B are connected or not. The common parent element is MyClass <A> and MyClass <B> Object.
Unless you explicitly define a relationship. for example, support an implicit conversion operator:
class Box<T> { func __conversion<U>() -> Box<U> { // do whatever required for conversion // try reinterpretCast if you can't make type system happy // which will give you runtime error if you do it wrong... return Box<U>() } }
source share