ReactiveX / RxScala

RxScala – Reactive Extensions for Scala – a library for composing asynchronous and event-based programs using observable sequences
Apache License 2.0
888 stars 110 forks source link

Conversions from Scala's Try and Option to Observable #209

Closed rvanheest closed 8 years ago

rvanheest commented 8 years ago

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:

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

zsxwing commented 8 years ago

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!

rvanheest commented 8 years ago

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!

rvanheest commented 8 years ago

Closing, as this was solved in #210