我基本上是想:
SpecialSequence
和SpecialGenerator
SpecialGenerator
实施为SpecialGen
SpecialSequence
这是我到目前为止所尝试的内容:
protocol SpecialGenerator: Generator {
typealias T;
func next() -> T?;
//...
}
class SpecialGen<T>: SpecialGenerator {
var Objects: Array<T>;
var CurrIndex = 0;
init(objs: Array<T>) {
self.Objects = objs;
}
func next() -> T? {
return Objects[CurrIndex];
}
//...
}
protocol SpecialSequence: Sequence {
typealias T;
func generate() -> SpecialGenerator;
//...
}
extension Array: SpecialSequence {
func generate() -> SpecialGenerator {
return SpecialGen(objs: self);
}
}
但我收到错误Array<T> does not conform to protocol "SpecialSequence"
我也尝试使用SpecialGenerator
和SpecialGen<T>
的各种组合作为返回类型无效。
这可以在Swift中使用吗?
答案 0 :(得分:0)
您需要为您定义的类型别名提供值
protocol SpecialSequence: Sequence {
typealias Foo;
func generate() -> SpecialGenerator;
//...
}
extension Array: SpecialSequence {
typealias Foo = T
func generate() -> SpecialGenerator {
return SpecialGen(objs: self);
}
}
这为我编译