Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Fix the compose covariance#1577

Merged
benjchristensen merged 2 commits intoReactiveX:masterfrom
zsxwing:compose-covariance
Aug 14, 2014
Merged

Fix the compose covariance#1577
benjchristensen merged 2 commits intoReactiveX:masterfrom
zsxwing:compose-covariance

Conversation

@zsxwing
Copy link
Member

@zsxwing zsxwing commented Aug 14, 2014

Changed the return type of compose from Observable<? extends R> to Observable<R>. The reason is that if returning Observable<? extends R>, the following codes won't work in Java:

        Observable<String> o1 = Observable.from("s1");
        Observable<String> o2 = o1.compose(new Transformer<String, String>() {
            @Override
            public Observable<? extends String> call(Observable<? extends String> t1) {
                return Observable.from("s2");
            }
        });

we have to write:

        Observable<String> o1 = Observable.from("s1");
        Observable<? extends String> o2 = o1.compose(new Transformer<String, String>() {
            @Override
            public Observable<? extends String> call(Observable<? extends String> t1) {
                return Observable.from("s2");
            }
        });

Because we cannot assign Observable<? extends R> to a Observable<R> variable, we should avoid to return Observable<? extends R>, otherwise the API will be inconvenient.

@cloudbees-pull-request-builder

RxJava-pull-requests #1487 FAILURE
Looks like there's a problem with this pull request

@zsxwing
Copy link
Member Author

zsxwing commented Aug 14, 2014

Although it's not elegant, it's the best way I can find. Any suggestion?

@cloudbees-pull-request-builder

RxJava-pull-requests #1488 SUCCESS
This pull request looks good

@headinthebox
Copy link
Contributor

Less variance is more elegant IMHO.

@abersnaze
Copy link
Contributor

I was able to git rid of the ? extends by making look kind of like defer.

\-    public <R> Observable<? extends R> compose(Transformer<? super T, ? extends R> transformer) {
\-        return transformer.call(this);
\+    public <R> Observable<R> compose(final Transformer<? super T, ? extends R> transformer) {
\+        final Observable<T> self = this;
\+        return new Observable<R>(new OnSubscribe<R>() {
\+            @Override
\+            public void call(Subscriber<? super R> child) {
\+                transformer.call(self).unsafeSubscribe(child);
\+            }
\+        });
     }

@benjchristensen
Copy link
Member

@abersnaze That's another layer of object allocation and subscription ... what do we gain with this?

Is ? extends R necessary?

benjchristensen added a commit that referenced this pull request Aug 14, 2014
@benjchristensen benjchristensen merged commit b0c98a5 into ReactiveX:master Aug 14, 2014
@zsxwing zsxwing deleted the compose-covariance branch October 3, 2014 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants