Skip to content

Instantly share code, notes, and snippets.

@jmhofer
Forked from anonymous/Main.java
Created March 19, 2013 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmhofer/5195589 to your computer and use it in GitHub Desktop.
Save jmhofer/5195589 to your computer and use it in GitHub Desktop.
// plain old Java
import rx.Observable;
import rx.util.functions.Action1;
public class Main {
public static void main(String[] args) {
String[] names = new String[] {
"foo", "bar", "baz"
};
// Compiles, but does nothing... (!)
// The compiler really should only accept Action1<String> here.
Observable.toObservable(names).subscribe(new Action1<Integer>() {
@Override public void call(Integer i) {
System.out.println(i);
}
});
}
}
// Scala, without using the language adaptor
import rx.Observable
import rx.util.functions.Action1
import scala.collection.JavaConverters._
object MainScala extends App {
val names = Seq("foo", "bar", "baz")
// Works as expected
Observable toObservable names.asJava subscribe new Action1[String]() {
def call(str: String): Unit = println(str)
}
// Compiles, but fails silently...
// The compiler really should only accept Action1[String] here.
Observable toObservable names.asJava subscribe new Action1[Int]() {
def call(i: Int): Unit = println(i)
}
}
// Scala, using the language adaptor
// How I want to be able to use "subscribe"
import rx.Observable
import rx.util.functions.Action1
import scala.collection.JavaConverters._
object MainScalaAdaptor extends App {
val names = Seq("foo", "bar", "baz")
// Leads to runtime exception: "Unsupported closure type: BoxedUnit"
Observable toObservable names.asJava subscribe println
}
// Scala, using the language adaptor
import rx.Observable
import rx.util.functions.Action1
import scala.collection.JavaConverters._
object MainScalaAdaptor2 extends App {
val names = Seq("foo", "bar", "baz")
// not exactly pretty, but works
Observable toObservable names.asJava subscribe { (str: String) => println(str) }
// compiles, but silently fails...
Observable toObservable names.asJava subscribe { (i: Int) => println(i) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment