[swift-users] Concrete Collection/Generator protocol and type

Stefan Urbanek stefan.urbanek at gmail.com
Thu Dec 10 00:15:49 CST 2015


Hi,

I would like to have various kinds of generators and eventually their collection counterparts of a concrete type, say Card. This is an example with generators to illustrate my intention. 

typealias Card = Int
typealias Predicate = Int

protocol CardGenerator: GeneratorType {
    typealias Element = Card
    // + some common functions to the protocol
}

class ConcreteGenerator: CardGenerator {
    typealias Element = Card

    // some complex element fetching, some implicit filtering and transforms are here
    func next() -> Element? { return nil }
}

class FilteredGenerator: CardGenerator {
    typealias Element = Card
    let predicate: Predicate

    init(predicate: Predicate) {
        self.predicate = predicate
    }

    // some complex element fetching, multiple filters and transforms are here
    func next() -> Element? { return nil }
}

func select(predicate:Predicate?) → CardGenerator {
    if predicate != nil {
        return FilteredGenerator(predicate:predicate!)
    }
    else {
        return ConcreteGenerator()
    }
}

I am getting:

"error: protocol 'CardGenerator' can only be used as a generic constraint because it has Self or associated type requirements”

How can I have a non-generic collection or generator protocol with specific element type tied to the protocol?


Thanks,

Stefan


More information about the swift-users mailing list