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

Generics Help Please? #858

Closed
benjchristensen opened this issue Feb 12, 2014 · 5 comments
Closed

Generics Help Please? #858

benjchristensen opened this issue Feb 12, 2014 · 5 comments

Comments

@benjchristensen
Copy link
Member

Generics have me stumped on the merge operator. The functionality is fine, but only if I leave it untyped.

All other operators I'm using lift with are okay, but I can't get this one.

// this doesn't compile
    public final static <T> Observable<T> merge(Observable<? extends Observable<? extends T>> source) {
        return source.lift(new OperatorMerge<T>()); 
    }

// this compiles with untyped warnings (no generic) and functions
    public final static <T> Observable<T> merge(Observable<? extends Observable<? extends T>> source) {
        return source.lift(new OperatorMerge()); 
    }

// this compiles (but is only merging itself so not very helpful)
    public Observable<T> mergeSelf() {
        return from(this).lift(new OperatorMerge<T>());
    }

// this doesn't compile
    public Observable<T> mergeSelfWithAnother(Observable<? extends T> other) {
        return from(this, other).lift(new OperatorMerge<T>());
    }

Here is the compilation error:

/Users/bechristensen/development/github/RxJavaFork/rxjava-core/src/main/java/rx/Observable.java:1780: error: method lift in class Observable<T#1> cannot be applied to given types;
        return source.lift(new OperatorMerge<T>());
                     ^
  required: Operator<R,CAP#1>
  found: OperatorMerge<T#2>
  reason: no instance(s) of type variable(s) R exist so that argument type OperatorMerge<T#2> conforms to formal parameter type Operator<R,CAP#1>
  where R,T#1,T#2 are type-variables:
    R extends Object declared in method <R>lift(Operator<R,T#1>)
    T#1 extends Object declared in class Observable
    T#2 extends Object declared in method <T#2>merge(Observable<? extends Observable<? extends T#2>>)
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Observable<? extends T#2> from capture of ? extends Observable<? extends T#2>
@benjchristensen
Copy link
Member Author

Thank you to @abersnaze for fixing the generics!

@IgorGanapolsky
Copy link

I am getting the same compilation error now. Using io.reactivex:rxjava:1.0.10. What is the solution?

@akarnokd
Copy link
Member

akarnokd commented Aug 6, 2015

@IgorGanapolsky what is the code exactly that doesn't compile?

@IgorGanapolsky
Copy link

@akarnokd I have this code:
return plugshareService.getLocationInfo(locationId). map(new PlugshareResponseToLocation()). subscribeOn(Schedulers.io()). observeOn(AndroidSchedulers.mainThread());

And I get this error:

Error:(28, 60) error: method map in class Observable cannot be applied to given types;
required: Func1<? super EVgoLocation,? extends R>
found: PlugshareResponseToLocation
reason: cannot infer type-variable(s) R
(argument mismatch; PlugshareResponseToLocation cannot be converted to Func1<? super EVgoLocation,? extends R>)
where R,T are type-variables:
R extends Object declared in method map(Func1<? super T,? extends R>)
T extends Object declared in class Observable

@IgorGanapolsky
Copy link

Nevermind, I solved it. I have to use this in my Retrofit Observable interface:
@POST("/locations/{id}") Observable<PlugshareResponse> getLocationInfo(@Path("id") String id);

instead of
@POST("/locations/{id}") Observable<EVgoLocation> getLocationInfo(@Path("id") String id);

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

No branches or pull requests

3 participants