我想从这样的sObservable
数组中创建一个:Observable
package rxtest
import concurrent._
import concurrent.ExecutionContext.Implicits.global
import rx.lang.scala._
import rx.lang.scala.JavaConversions._
import rx.lang.scala.schedulers._
object A extends App {
val ps = Array.fill(3)(Promise[Int]())
val os = ps map {
p => Observable from p.future observeOn NewThreadScheduler()
}
val v = rx.Observable.merge(os map toJavaObservable)
}
该程序无法编译,因为Observable
有几个重载方法都被调用merge
:
[error] /home/xgp/work/rxtest/src/main/scala/rxtest/A.scala:15: overloaded method value merge with alternatives:
[error] [T](x$1: Array[rx.Observable[_ <: T]])rx.Observable[T] <and>
[error] [T](x$1: rx.Observable[_ <: rx.Observable[_ <: T]])rx.Observable[T] <and>
[error] [T](x$1: Iterable[_ <: rx.Observable[_ <: T]])rx.Observable[T]
[error] cannot be applied to (Array[rx.Observable[_ <: Int]])
[error] val v = rx.Observable.merge(os map toJavaObservable)
[error] ^
[error] one error found
然后我想在另一个 Java 类的帮助下消除重载:
public class RxUtils {
public final static <T> Observable<T> merge(Observable<? extends T>[] os) {
return Observable.merge(os);
}
}
Scala 代码变为(此处仅列出相关部分):
val ps = Array.fill(3)(Promise[Int]())
val os = ps map {
p => Observable from p.future observeOn NewThreadScheduler()
}
val v = RxUtils.merge(os map toJavaObservable)
该程序仍然无法编译:
[error] /home/xgp/work/rxtest/src/main/scala/rxtest/A.scala:17: no type parameters for method merge: (os: Array[rx.Observable[_ <: T]])rx.Observable[T] exist so that it can be applied to arguments (Array[rx.Observable[_ <: Int]])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : Array[rx.Observable[_ <: Int]]
[error] required: Array[rx.Observable[_ <: ?T]]
[error] val v = RxUtils.merge(os map toJavaObservable)
[error] ^
[error] /home/xgp/work/rxtest/src/main/scala/rxtest/A.scala:17: type mismatch;
[error] found : Array[rx.Observable[_ <: Int]]
[error] required: Array[rx.Observable[_ <: T]]
[error] val v = RxUtils.merge(os map toJavaObservable)
[error] ^
[error] two errors found
我有三个问题:
- 如何
merge
像第一种情况一样用纯 Scala 调用方法? - 为什么第二个程序无法编译?
- 如何在Scala中调用
merge
上述类中的方法?RxUtils