我尝试使用 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
}
}
我怎样才能做到这一点?
最佳答案
SequenceType
的 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 { $0.map { seq in seq.map(translator.translate) } }
}
}
关于swift - 如何扩展ReactiveCocoa4的SignalProducerType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522830/