Generics 引用具有关联类型的实例实现协议 问题
我按照(第4个回答)解决了通用协议的不足:我试图实现一个类似于/Generics 引用具有关联类型的实例实现协议 问题,generics,swift,Generics,Swift,我按照(第4个回答)解决了通用协议的不足:我试图实现一个类似于/SequenceOf-的结构的生成器,它可以保存对实现我的ExampleType协议的任何实例的引用。由于协议使用关联的类型,我不能直接将其用作变量的类型,需要一个包装器结构 在我遇到一个泛型函数之前,它一直工作得很好。我无法创建一个用于转发此函数的闭包,因此无法像Apple在GeneratorOf中那样完全实现我的包装结构 现状 这里是一个简化但不完整的示例,它在Xcode 6.1 GM 2上编译得很好,但缺少通用函数: prot
SequenceOf
-的结构的生成器,它可以保存对实现我的ExampleType协议的任何实例的引用。由于协议使用关联的类型,我不能直接将其用作变量的类型,需要一个包装器结构
在我遇到一个泛型函数之前,它一直工作得很好。我无法创建一个用于转发此函数的闭包,因此无法像Apple在GeneratorOf
中那样完全实现我的包装结构
现状
这里是一个简化但不完整的示例,它在Xcode 6.1 GM 2上编译得很好,但缺少通用函数:
protocol ExampleType {
typealias Element
func functionWithElement(element: Element)
func functionWithSequence<S: SequenceType where S.Generator.Element == Element>(sequence: S)
}
struct ExampleOf<T>: ExampleType {
typealias Element = T
private let _functionWithElement: (T) -> ()
// how to declare _functionWithSequence?
init<E: ExampleType where E.Element == T>(_ base: E) {
_functionWithElement = { base.functionWithElement($0) }
// how to assign _functionWithSequence?
}
func functionWithElement(element: Element) {
_functionWithElement(element)
}
func functionWithSequence<S: SequenceType where S.Generator.Element == Element>(sequence: S) {
// how to call _functionWithSequence?
}
}
协议示例类型{
类型别名元素
func functionWithElement(元素:元素)
func functionWithSequence(序列:S)
}
结构ExampleOf:ExampleType{
typealias元素=T
私有let _functionWithElement:(T)->()
//如何用sequence声明函数?
初始值(u基:E){
_functionWithElement={base.functionWithElement($0)}
//如何分配_函数的顺序?
}
func functionWithElement(元素:元素){
_functionWithElement(元素)
}
func functionWithSequence(序列:S){
//如何用sequence调用函数?
}
}
天真的方法
这是它应该如何工作的,但在Swift中显然是不可能的
protocol ExampleType {
typealias Element
func functionWithElement(element: Element)
func functionWithSequence<S: SequenceType where S.Generator.Element == Element>(sequence: S)
}
struct ExampleOf<T>: ExampleType {
typealias Element = T
private let _functionWithElement: (T) -> ()
private let _functionWithSequence: <S: SequenceType where S.Generator.Element == Element>((S) -> ())
// ERROR: Only syntactic function types can be generic
init<E: ExampleType where E.Element == T>(_ base: E) {
_functionWithElement = { base.functionWithElement($0) }
_functionWithSequence = { base.functionWithSequence($0) }
}
func functionWithElement(element: Element) {
_functionWithElement(element)
}
func functionWithSequence<S: SequenceType where S.Generator.Element == Element>(sequence: S) {
_functionWithSequence(sequence)
}
}
协议示例类型{
类型别名元素
func functionWithElement(元素:元素)
func functionWithSequence(序列:S)
}
结构ExampleOf:ExampleType{
typealias元素=T
私有let _functionWithElement:(T)->()
私有let函数的顺序为:((S)->())
//错误:只有语法函数类型可以是泛型的
初始值(u基:E){
_functionWithElement={base.functionWithElement($0)}
_functionWithSequence={base.functionWithSequence($0)}
}
func functionWithElement(元素:元素){
_functionWithElement(元素)
}
func functionWithSequence(序列:S){
_功能与顺序(顺序)
}
}
问题:
我如何克服这个限制?如果只是在包装器内部,我不介意在这里和那里进行强制转换。您所要做的就是使用
SequenceOf
包装参数:
协议示例类型{
类型别名元素
func functionWithElement(元素:元素)
func functionWithSequence(序列:S)
}
结构ExampleOf:ExampleType{
私有let _functionWithElement:(T)->()
私有let _functionWithSequence:(SequenceOf)->()
初始值(u基:E){
_functionWithElement={base.functionWithElement($0)}
_functionWithSequence={base.functionWithSequence($0)}
}
func functionWithElement(元素:T){
_functionWithElement(元素)
}
func functionWithSequence(序列:S){
_功能与顺序(SequenceOf(sequence))
}
}
TypeAlias可能尚未涉及声明关联类型的泛型或协议。你必须对这项功能进行雷达扫描。我知道目前没有直接的方法可以做到这一点。因为我不想等待苹果公司半年来解决这个问题,所以我要求一个解决方案来实现同样的结果。也许你没有完全理解我的意思:你被禁止在类型别名和成员变量之间进行任何量化,因为Swift的类型系统中根本没有规则来处理它。如果你有存在主义类型或秩-N多态性的阴影,这个讨论会更有成果。现在,不幸的是,在它们进一步迭代之前,您一直处于停滞状态。斯威夫特的类型需要尽早且经常地具体化,这意味着你需要用填鸭式的方式检查类型,或者用不安全的Bitcast()处理类似的事情。谢谢你的澄清。我会玩一些unsafeBitCast,但我不是很有信心我会找到一个解决办法。
protocol ExampleType {
typealias Element
func functionWithElement(element: Element)
func functionWithSequence<S: SequenceType where S.Generator.Element == Element>(sequence: S)
}
struct ExampleOf<T>: ExampleType {
private let _functionWithElement: (T) -> ()
private let _functionWithSequence: (SequenceOf<T>) -> ()
init<E: ExampleType where E.Element == T>(_ base: E) {
_functionWithElement = { base.functionWithElement($0) }
_functionWithSequence = { base.functionWithSequence($0) }
}
func functionWithElement(element: T) {
_functionWithElement(element)
}
func functionWithSequence<S: SequenceType where S.Generator.Element == T>(sequence: S) {
_functionWithSequence(SequenceOf(sequence))
}
}