Skip to content
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

Compose/Transform Covariance #1569

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ public void call(Subscriber<? super R> o) {
* @param transformer
* @return
*/
public <R> Observable<R> compose(Transformer<T, R> transformer) {
public <R> Observable<? extends R> compose(Transformer<? super T, ? extends R> transformer) {
return transformer.call(this);
}

/**
* Transformer function for `compose`
*/
public static interface Transformer<T, R> extends Func1<Observable<T>, Observable<R>> {
public static interface Transformer<T, R> extends Func1<Observable<? extends T>, Observable<? extends R>> {
// cover for generics insanity
}

Expand Down
27 changes: 27 additions & 0 deletions rxjava-core/src/test/java/rx/CovarianceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.junit.Test;

import rx.Observable.Transformer;
import rx.functions.Func2;

/**
Expand Down Expand Up @@ -57,6 +58,32 @@ public Integer call(Media t1, Media t2) {
o2.toSortedList(SORT_FUNCTION);
}


@Test
public void testCovarianceOfCompose() {
Observable<HorrorMovie> movie = Observable.<HorrorMovie> from(new HorrorMovie());
movie.compose(new Transformer<Movie, Movie>() {

@Override
public Observable<? extends Movie> call(Observable<? extends Movie> t1) {
return Observable.from(new Movie());
}

});
}

@Test
public void testCovarianceOfCompose2() {
Observable<Movie> movie = Observable.<Movie> from(new HorrorMovie());
movie.compose(new Transformer<Movie, Movie>() {
@Override
public Observable<? extends Movie> call(Observable<? extends Movie> t1) {
return Observable.from(new HorrorMovie());
}
});
}


/*
* Most tests are moved into their applicable classes such as [Operator]Tests.java
*/
Expand Down
4 changes: 2 additions & 2 deletions rxjava-core/src/test/java/rx/ObservableTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1108,9 +1108,9 @@ public void testCompose() {
Observable.from(1, 2, 3).compose(new Transformer<Integer, String>() {

@Override
public Observable<String> call(Observable<Integer> t1) {
public Observable<? extends String> call(Observable<? extends Integer> t1) {
return t1.map(new Func1<Integer, String>() {

@Override
public String call(Integer t1) {
return String.valueOf(t1);
Expand Down