-
-
Save jmhofer/5195589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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