ReactiveX/RxScala

Conversions from Scala's Try and Option to Observable

rvanheest opened this issue · 3 comments

Would it make any sense to have conversion methods from scala.util.Try and scala.Option to rx.lang.scala.Observable? I mean, there is a conversion from Iterable to Observable in rx.lang.scala.ObservableExtensions, which I view as the Scala equivalent of RxJava's Observable.from.

Java does not really have type level notions of Try and Option (the latter only in Java 8), so it makes sense that RxJava does not have them. Scala, however, does have these types and in my view they should have a similar conversion method as Iterable.

The implementations would of course be very simple:

  • Option
    • Some(v) -> OnNext(v) followed by OnCompleted
    • None -> OnCompleted
  • Try
    • Success(v) -> OnNext(v) followed by OnCompleted
    • Failure(e) -> OnError(e)

LMKWYT! I'd be more than happy to do another PR!

It's not necessary to add an Option overload. It's already supported. See

scala> Observable.from(Some(1)).foreach(println)
1

Try is missing. So PR is welcome!

Agree kind of agree on the Option, although it seems that you can't do Some(1).toObservable with that. I think that is because the compiler has to do 2 implicit conversions for that: the Iterable[T] to Option[T] and the toObservable conversion. So that's why I suggested to do the Option part as well.

Regarding the Try, I have made a similar thing as the ObservableExtensions in the package object, so the syntax would be Try(...).toObservable. But now that I'm thinking about it, maybe you would like to have the Observable.from(Try(...)) alternative as well... I'll add this last bit and do a PR. Then we'll see what we can use and what can be left out!

Closing, as this was solved in #210