Description
RxJava was written from the beginning to target the JVM, not any specific language.
As a side-effect of Java not having lambdas/clojures yet (and other considerations), Netflix used dynamic languages with it predominantly for the year of its existence prior to open sourcing.
To bridge the rxjava-core written in Java with the various languages a FunctionalLanguageAdaptor is registered at runtime for each language of interest.
To enable this language adaptors method overloads with Object
exist in the API since Object
is the only super-type that works across all languages for their various implementations of lambdas and closures.
This downside of this is that it breaks static typing for Java, Scala and other static-typed languages. More can be read on this issue and discussion of the subject here: https://groups.google.com/forum/#!topic/rxjava/bVZoKSsb1-o
I would like to pursue the following:
- remove all
Object
overload methods from rxjava-core so it remains statically typed - modify FunctionalLanguageAdaptors to not register with a lookup map but instead to trigger a code generation process at registration time
- the code generation would create the overload methods on
rx.Observable
for the types needed by that languages (such asgroovy.lang.Closure
orclojure.lang.IFn
).
The benefits of this are:
- Everything is statically typed so compile-time checks for Java, Scala, etc work correctly
- Method dispatch is now done via native Java bytecode using types rather than going via
Object
which then has to do a lookup in a map. Memoization helped with the performance but each method invocation still required looking in a map for the correct adaptor. With this approach the appropriate methods will be compiled into therx.Observable
class to correctly invoke the right adaptor without lookups.
The byte code generation step would happen once the first time a LanguageAdaptor is registered which is normally only at the first point of access to RxJava.