-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RxScala <-> RxJava converters #207
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea, and it seems to be independent from the JavaConversions, so the two approaches could coexist.
I'd like to hear also @zsxwing's opinion.
Before this can be merged, there should be a test-and-example file for it, similar to RxScalaDemo.
The goal is not to test the functionality (that's trivial), but it's to test the signatures: That they can be used the in the intended way, without overloading problems, conflicting implicits, or implicit conversions not kicking in, etc... There's plenty of things which could go wrong here, especially at a later point when the library evolves. And moreover, the tests can also serve as a guide with usage examples.
I'd suggest that you create a new test file, in the same directory as the RxScalaDemo, where you show by some examples how the asScala
/asJava
extension methods can be used. And ideally, you'd also explain that there's a second approach in JavaConversions
.
|
||
class AsJavaSubscription(s: Subscription) { | ||
def asJavaSubscription: rx.Subscription = s.asJavaSubscription | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you use asJava
for subscriptions as well?
* val javaObs = Observable.just(1, 2, 3).asJava | ||
* val scalaObs = javaObs.asScala | ||
* }}} | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation should be one space before the *
*/ | ||
object JavaConverters extends DecorateAsJava with DecorateAsScala | ||
|
||
private[scala] trait Decorators { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(sorry for nitpicking) Code indentation in this project is 2 spaces, could you please adhere to this?
in this response the RxJava variants of the types are prefixed with a Let me reply to your questions: Could I add some examples similar to spaces instead of tabs - fixed that Why don't I use Hope this clears up any of your questions. |
val sSubscription: Subscription = jSubscription.asScalaSubscription | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an excellent demo 😃
I tried to understand why we can't use |
Yup, I got a similar error yesterday in the last example as well (I run Scala 2.11.8). Asked about it on https://gitter.im/scala/scala and got a response from @SethTisue (see below). If I find time in the next couple of days, I'll have a look at this compiler bug. Let's keep in touch on this! In the meantime I'll fix the |
Since |
@zsxwing I did not know that, but indeed read this in the scaladoc. Maybe this PR could provide a replacement for this. Although I really think that deprecating @samuelgruetter I tried to come up with a better solution than using separate keywords for Other than that, I consider this PR as a nice alternative for |
Agreed. |
I isolated the scalac bug: import scala.language.implicitConversions
object Bug extends App {
class JObserver[T]
class JSubscriber[T] extends JObserver[T]
class Foo
implicit def convertSubscriber[T](s: JSubscriber[_ >: T]): Foo = ???
implicit def convertObserver[T](s: JObserver[_ >: T]): Foo = ???
val jSubscriber: JSubscriber[_ >: Int] = ???
val foo: Foo = jSubscriber // compiler crashes
} I think it's SI-5559, and it seems that there is no simple work-around... So I guess we have to live with the more verbose |
My pleasure, mate! You guys are doing great stuff and I'm happy to contribute. |
Hi, based on my question on StackOverflow and the answer/invite by @samuelgruetter I got to work and created a
JavaConverters
class that is similar to therx.lang.scala.JavaConversions
class but does the conversion between RxJava and RxScala by using theasScala
method (andasJava
vise versa). The implementation is similar to the one in the Scala SDK (scala.collections.JavaConverters
) and offers a way to do a more explicit implicit conversion.Usage: although the use is quite obvious, I included an example of the intended use below.
Given a function that return a RxJava
Observable
(most likely coming from a Java based API), you transform it to a RxScalaObservable
usingasScala
:Similarly, you convert a RxScala
Observable
to an RxJavaObservale
usingasJava
:Besides
Observable
I included similar conversions for all types that were already inrx.lang.scala.JavaConversions
(Observer
,Scheduler
,Worker
, etc.).