我有一个类SomeController<A where A: ProtA>
我有一些类似SubController: SomeController<SubA>
以下是我尝试输入检查的一个工作示例:
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"
从功能上来说,代码完全符合我的要求,但是,我留下了大量警告Cast from SuperClass<A> to SubClass always fails
显然,这些类型是相关的,显然代码运行良好并且不会“总是失败”,所以这个警告似乎是错误的。这是类型系统的当前限制或边缘情况,还是有办法使警告消失?
答案 0 :(得分:0)
定义SuperClass,因为它自己的通用会抑制警告。我也认为你不需要where子句来指定A符合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")
}
}