如何扩展 ReactiveCocoa4 的 SignalProducerType

How to extend SignalProducerType of ReactiveCocoa4

我尝试使用 SequenceType 值扩展 SignalProducerType。但是我做不到。 'Type of expression is ambiguous without more context' 编译出现错误。

protocol TranslatorType {
    typealias Source
    typealias Destination
    func translate(source: Source) -> Destination
}

extension SignalProducerType where Value: SequenceType {
    func translate<T: TranslatorType, U: SequenceType where T.Source == Value.Generator.Element, T.Destination == U.Generator.Element>(translator: T) -> ReactiveCocoa.SignalProducer<U, Error> {
        return lift { [=10=].map { seq in seq.map(translator.translate) } } # Type of expression is ambiguous without more context error
    }
}

我该怎么做?

SequenceType returns 数组的 map 函数,不能映射到泛型 SequenceType。把类型U改成数组,可以编译:

extension SignalProducerType where Value: SequenceType {
    func translate<T: TranslatorType, V where T.Source == Value.Generator.Element, T.Destination == V>(translator: T) -> ReactiveCocoa.SignalProducer<[V], Error> {
        return lift {  [=10=].map { seq in seq.map(translator.translate) } }
    }
}