简体   繁体   English

从非协议,非类类型“ V”继承

[英]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.

相关问题 从非协议,非类类型&#39;CGPoint&#39;继承 - Inheritance from non-protocol, non-class type 'CGPoint' 来自非协议类型“PFObject”的 Inheritance - Inheritance from non-protocol type 'PFObject' Inheritance 来自非协议类型 'UICollectionViewFlowLayout' - Inheritance from non-protocol type 'UICollectionViewFlowLayout' 在Swift中,如何制作String的后代? 尝试执行此操作时出现错误“从非协议,非类类型&#39;String&#39;继承” - In Swift, how to make a descendant of String? I get error “Inheritance from non-protocol, non-class type 'String'” when trying to do that inheritance 来自非协议类型“UIViewController”@objc 协议用户:UIViewController - inheritance from non-protocol type 'UIViewController' @objc protocol User: UIViewController SwiftUI View 入门代码在添加到现有项目时抱怨“从非协议类型“View”继承” - SwiftUI View starter code complains about `inheritance from non-protocol type 'View'` when added to an existing project 如何使非类类型符合类协议? - How to make a non-class type conform to a class protocol? swift 协议“弱”不能应用于非类类型 - swift protocol 'weak' cannot be applied to non-class type <unknown> :0:错误:类型“键”被限制为非协议类型“字符串” - <unknown>:0: error: type 'Key' constrained to non-protocol type 'String' Structs上的协议扩展导致编译错误“Self”约束为非协议类型 - Protocol extensions on Structs causes compile error 'Self' constrained to non-protocol type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM