[英]Inheritance from non-protocol, non-class type 'V'
I'm trying to define a class like 我正在尝试定义一个像
class x<V,M : V> {}
Which V will be a protocol when defining a new class that extends x but compiler says: 定义扩展x的新类时,哪个V将是协议,但编译器会说:
Inheritance from non-protocol, non-class type 'V'
从非协议,非类类型“ V”继承
My real example: 我的真实例子:
class ListController
<RequestModel, FillerProtocol , ResultModel, Cell>: BaseViewController
where
ResultModel : FillerProtocol,
Cell: BaseCell<FillerProtocol>,
RequestModel: ExtendableByBaseListRequestModel {}
Which cause this error: 导致此错误的原因:
Type 'ResultModel' constrained to non-protocol, non-class type 'FillerProtocol'
类型“ ResultModel”限制为非协议,非类类型“ FillerProtocol”
What you are trying to accomplish is not possible using generics. 使用泛型是不可能实现的。 First you'd have to be able to express that your generic parameter
V
is a protocol or a non-final class. 首先,您必须能够表达您的通用参数
V
是协议还是非最终类。 This is not possible. 这是不可能的。 But this restriction is necessary for the constraint
M: V
to make any sense. 但是,此约束对于使约束
M: V
有意义是必要的。
You might have more luck modeling your problem using protocols with associated types, maybe like this: 您可能会更幸运地使用具有关联类型的协议来对问题进行建模,例如:
protocol FillerProtocol {
associatedtype ResultModel
}
class ListController<RequestModel, Filler: FillerProtocol, Cell>: BaseViewController
where
Cell: BaseCell<Filler>,
RequestModel: ExtendableByBaseListRequestModel
{
}
That way you don't even have to specify your ResultModel
type, it is defined by your FillerProtocol
. 这样,您甚至不必指定
ResultModel
类型,它由FillerProtocol
定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.