Type verification class with typical types

I have a class like SomeController<A where A: ProtA>

I have some subclasses of type SubController: SomeController<SubA>

Here is a working example of how I am trying to enter validation:

 protocol SomeProtocol { } class SuperClass<A where A: SomeProtocol> { } class SubProtocol: SomeProtocol { } class SubClass: SuperClass<SubProtocol> { } func classTest<A where A: SomeProtocol>(classToTest: SuperClass<A>) { switch classToTest { case is SubClass: // Has warning 'Cast from SuperClass<A> to SubClass always fails' print("I'm a SubClass") default: print("Wasn't found") } } classTest(SubClass()) // Prints "I'm a SubClass" 

Functionally, the code does exactly what I want, however I am left with a ton of warnings saying Cast from SuperClass<A> to SubClass always fails

Itโ€™s clear that the types are related, and obviously the code works fine and doesnโ€™t โ€œalways failโ€, so this warning seems to be wrong. Is this the current constraint or edge case of the type system, or is there a way to make the warning go away?

+5
source share
1 answer

The SuperClass definition, since it is a generic one, suppresses the warning. I also don't think you need a where clause to indicate that A matches SomeProtocol.

 func classTest<A:SomeProtocol, B:SuperClass<A>>(classToTest: B, useA:A) { switch classToTest { case is SubClass: // Has warning 'Cast from SuperClass<A> to SubClass always fails' print("I'm a SubClass") default: print("Wasn't found") } } 
0
source

All Articles