我正试图封装rxjava的timeout
方法,以便将其用于scala。
类似于许多其他方法,我尝试了以下代码:
def timeout[U >: T](timeout: Duration, other: Observable[U]): Observable[U] = {
val otherJava: rx.Observable[_ <: U] = other.asJavaObservable
val thisJava: rx.Observable[_ <: U] = this.asJavaObservable
toScalaObservable[U](thisJava.timeout(timeout.length, timeout.unit, otherJava))
}
但是我遇到了以下错误:
Observable.scala:1631: error: overloaded method value timeout with alternatives:
($1: Long,x$2: java.util.concurrent.TimeUnit,x$3: rx.Scheduler)rx.Observable[_$85] <and>
($1: Long,x$2: java.util.concurrent.TimeUnit,x$3: rx.Observable[_ <: _$85])rx.Observable[_$85]
cannot be applied to (Long, scala.concurrent.duration.TimeUnit, rx.Observable[_$84])
toScalaObservable[U](thisJava.timeout(timeout.length, timeout.unit, otherJava))
原始的Java方法:
public Observable<T> timeout(long timeout, TimeUnit timeUnit, Observable<? extends T> other) {
return create(OperationTimeout.timeout(this, timeout, timeUnit, other));
}
我对Java和Scala (以及所有类型边界) 都不是很熟悉,但我理解:无论是otherJava
还是thisJava
都是类型为rx.Observable[U]
,那么它们为什么不能对齐呢?
U
类型参数,它甚至无法编译,因为协变类型不能出现在逆变位置(在other
中)。我删除了我的第一个观点并更新了代码。 - gourlaysamaList
来测试一下,它也是协变的:http://paste.ubuntu.com/6692064/ 这就是为什么我认为它们是完全相同的类型。仍然很困惑。 - ClojureMostly