コレクションに基づいてランダムジェネレーターを提供するクラスがあります。
これはランダム ジェネレーターであるため (コレクションが空でない限り、next() は nil を返すことはありません)、このジェネレーターを sequenceType として使用できるようにしたくありません (無限ループを回避するための「for in」サポートはありません)。
メソッドの署名を正しく取得できないようです。
これは私が構築したもののスケルトンです。対応するコンパイラ エラーを含む 3 つの試行を含めました。
public protocol myProtocol {
var name : String { get }
}
internal struct myProtocolStruct: myProtocol {
let name : String
init(name: String) {
self.name = name
}
}
internal struct myGenerator : GeneratorType {
let names : [myProtocol]
init(names: [myProtocol]) {
self.names = names
}
mutating func next() -> myProtocol? {
return names.first
}
}
public class myClass {
private var items : [myProtocol]
public init() {
let names = ["0", "1", "2"]
items = names.map{ myProtocolStruct(name: $0) }
}
public func generate0() -> GeneratorType { // error: protocol 'GeneratorType' can only be used as a generic constraint because it has Self or associated type requirements
let x = myGenerator(names: items)
return x
}
public func generate1<C: GeneratorType where C.Element == myProtocol>() -> C {
let x = myGenerator(names: items)
return x // error: 'myGenerator' is not convertible to 'C'
}
public func generate2<C: GeneratorType where C.Element: myProtocol>() -> C {
let x = myGenerator(names: items)
return x // error: 'myGenerator' is not convertible to 'C'
}
}