简体   繁体   English

扩展协议,其中 Self:Swift 中的泛型类型(需要 <...> 中的参数)

[英]Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <...>)

I have a class that takes a generic class Collection: <T: Model> ( Model is a class) and a protocol ( Resource ) that some of the subclasses of Collection implement:我有一个类,它采用一个泛型class Collection: <T: Model>Model是一个类)和一个Collection一些子类实现的协议( Resource ):

class Collection: <T: Model> {
  typealias Callback = (result: Collection <T>) -> ()
}
protocol Resource {...}

Is it possible to write a protocol extension where Self is an instance of Collection ?是否可以编写一个协议扩展,其中SelfCollection一个实例?

Trying to extend the protocol with the class that takes a generic:尝试使用采用泛型的类扩展协议:

extension Resource where Self: Collection {
  func fetch() {}
}

Gives:给出:

Reference to generic type 'Collection' requires arguments in <...>对泛型类型“Collection”的引用需要 <...> 中的参数

Trying to extend the class that takes a generic with the protocol:尝试使用协议扩展采用泛型的类:

extension Collection where Self: Resource {
  func fetch(callback: Callback?) {}
}

Gives:给出:

'Self' is only available in a protocol or as the result of method in a class “Self”仅在协议中可用或作为类中方法的结果可用

I'm not sure how to proceed.我不知道如何继续。 The goal is for the function to only be available on instances of Collection that conform to Resource .目标是使函数仅在符合ResourceCollection实例上可用。

The problem is Collection is a generic class so every where you declare it, you must attached the specialized type, like Collection<T> .问题是Collection是一个泛型类,所以在你声明它的每一个地方,你都必须附加专门的类型,比如Collection<T> However extension to a protocol can't be specified with a generic type so you end up not being able to supply T to Collection .但是,不能使用泛型类型指定协议的扩展,因此您最终无法将TCollection

In your case though, T is constrained to be of type Model so why not use that in the default protocol implementation:不过,在您的情况下, T被限制为Model类型,所以为什么不在默认协议实现中使用它:

extension Resource where Self: Collection<Model> {
    func fetch() {
        // default implementation
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用需要泛型的方法扩展Swift类型 - Extending Swift type with method that requires a generic 对泛型类型“Map”的引用需要 &lt;...&gt; 中的参数 - Swift - Reference to generic type 'Map' requires arguments in <…> - Swift 扩展Element类型为通用的Collection(Swift) - Extending a Collection where the Element type is generic (Swift) 具有泛型的Swift功能,其中约束是一个自我遵循的协议 - Swift func with generic where constraint is a protocol which self conforms 如何使用自引用Swift协议作为泛型类的类型 - How to use a self-referencing Swift protocol as a type for a generic class 具有自我的Swift通用协议功能 - Swift generic protocol function with self Swift错误:对泛型类型Dictionary的引用需要<...>中的参数 - Swift error: Reference to generic type Dictionary requires arguments in <…> Swift Generics:使用泛型类型的属性扩展非泛型类型,其中泛型参数是扩展类型 - Swift Generics: Extending a non-generic type with a property of generic type where the generic parameter is the extended type 实现符合Hashable协议的通用结构。 错误:对通用类型&#39;strA&#39;的引用需要&lt;…&gt;错误中的参数 - Implementing generic structs conforming to Hashable protocol. Error : Refernce to generic type 'strA' requires arguments in <…> error 带“ where Self”子句的Swift协议 - Swift protocol with “where Self” clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM