我尝试使用SequenceType值扩展SignalProducerType。但我不能这样做。在编译时出现'表达式类型不明确而没有更多上下文'错误。
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 { $0.map { seq in seq.map(translator.translate) } } # Type of expression is ambiguous without more context error
}
}
我该怎么做?
答案 0 :(得分:1)
map
SequenceType
函数会返回一个数组,您无法映射到通用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 { $0.map { seq in seq.map(translator.translate) } }
}
}