RxJava Subject unable to resolve generic consumer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Background:
The following code is from a generic bus from my code that will delegate object of different types (all of them are extending from BaseEvent class) to different consumers.
Problem:
private static PublishSubject<? extends BaseEvent> mPublishSubject;
@SuppressLint("CheckResult")
public void subscribeToEventBus(Class subscribingClass, Class eventToSubscribeTo, Consumer<? extends BaseEvent> subscriber) {
mPublishSubject.subscribe(subscriber);// <---- ERROR HERE
mEventBusSubscribersList.add(new EventBusInfo(subscribingClass, eventToSubscribeTo, subscriber));
}
Error:
error: no suitable method found for subscribe(Consumer<CAP#1>)
method Observable.subscribe(Consumer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Consumer<? super CAP#2>)
method Observable.subscribe(Observer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Observer<? super CAP#2>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends BaseEvent from capture of ? extends BaseEvent
CAP#2 extends BaseEvent from capture of ? extends BaseEvent
Objective:
I want Consumer<> to accept all the classes that extend from BaseEvent.
I'm not quite sure what exactly am I doing wrong considering my primitive knowledge of Java generics.
java generics rx-java2
add a comment |
Background:
The following code is from a generic bus from my code that will delegate object of different types (all of them are extending from BaseEvent class) to different consumers.
Problem:
private static PublishSubject<? extends BaseEvent> mPublishSubject;
@SuppressLint("CheckResult")
public void subscribeToEventBus(Class subscribingClass, Class eventToSubscribeTo, Consumer<? extends BaseEvent> subscriber) {
mPublishSubject.subscribe(subscriber);// <---- ERROR HERE
mEventBusSubscribersList.add(new EventBusInfo(subscribingClass, eventToSubscribeTo, subscriber));
}
Error:
error: no suitable method found for subscribe(Consumer<CAP#1>)
method Observable.subscribe(Consumer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Consumer<? super CAP#2>)
method Observable.subscribe(Observer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Observer<? super CAP#2>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends BaseEvent from capture of ? extends BaseEvent
CAP#2 extends BaseEvent from capture of ? extends BaseEvent
Objective:
I want Consumer<> to accept all the classes that extend from BaseEvent.
I'm not quite sure what exactly am I doing wrong considering my primitive knowledge of Java generics.
java generics rx-java2
Please provide the definition ofmPublishSubject.
– akarnokd
Nov 22 '18 at 13:10
@akarnokd I've updated the question
– Umer Farooq
Nov 22 '18 at 13:15
TryPublishSubject<BaseEvent>without the? extends. Java's generics sometimes doesn't allow certain co- and contravariant use cases.
– akarnokd
Nov 22 '18 at 15:00
add a comment |
Background:
The following code is from a generic bus from my code that will delegate object of different types (all of them are extending from BaseEvent class) to different consumers.
Problem:
private static PublishSubject<? extends BaseEvent> mPublishSubject;
@SuppressLint("CheckResult")
public void subscribeToEventBus(Class subscribingClass, Class eventToSubscribeTo, Consumer<? extends BaseEvent> subscriber) {
mPublishSubject.subscribe(subscriber);// <---- ERROR HERE
mEventBusSubscribersList.add(new EventBusInfo(subscribingClass, eventToSubscribeTo, subscriber));
}
Error:
error: no suitable method found for subscribe(Consumer<CAP#1>)
method Observable.subscribe(Consumer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Consumer<? super CAP#2>)
method Observable.subscribe(Observer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Observer<? super CAP#2>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends BaseEvent from capture of ? extends BaseEvent
CAP#2 extends BaseEvent from capture of ? extends BaseEvent
Objective:
I want Consumer<> to accept all the classes that extend from BaseEvent.
I'm not quite sure what exactly am I doing wrong considering my primitive knowledge of Java generics.
java generics rx-java2
Background:
The following code is from a generic bus from my code that will delegate object of different types (all of them are extending from BaseEvent class) to different consumers.
Problem:
private static PublishSubject<? extends BaseEvent> mPublishSubject;
@SuppressLint("CheckResult")
public void subscribeToEventBus(Class subscribingClass, Class eventToSubscribeTo, Consumer<? extends BaseEvent> subscriber) {
mPublishSubject.subscribe(subscriber);// <---- ERROR HERE
mEventBusSubscribersList.add(new EventBusInfo(subscribingClass, eventToSubscribeTo, subscriber));
}
Error:
error: no suitable method found for subscribe(Consumer<CAP#1>)
method Observable.subscribe(Consumer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Consumer<? super CAP#2>)
method Observable.subscribe(Observer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Observer<? super CAP#2>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends BaseEvent from capture of ? extends BaseEvent
CAP#2 extends BaseEvent from capture of ? extends BaseEvent
Objective:
I want Consumer<> to accept all the classes that extend from BaseEvent.
I'm not quite sure what exactly am I doing wrong considering my primitive knowledge of Java generics.
java generics rx-java2
java generics rx-java2
edited Nov 22 '18 at 13:15
Umer Farooq
asked Nov 22 '18 at 13:07
Umer FarooqUmer Farooq
5,63233254
5,63233254
Please provide the definition ofmPublishSubject.
– akarnokd
Nov 22 '18 at 13:10
@akarnokd I've updated the question
– Umer Farooq
Nov 22 '18 at 13:15
TryPublishSubject<BaseEvent>without the? extends. Java's generics sometimes doesn't allow certain co- and contravariant use cases.
– akarnokd
Nov 22 '18 at 15:00
add a comment |
Please provide the definition ofmPublishSubject.
– akarnokd
Nov 22 '18 at 13:10
@akarnokd I've updated the question
– Umer Farooq
Nov 22 '18 at 13:15
TryPublishSubject<BaseEvent>without the? extends. Java's generics sometimes doesn't allow certain co- and contravariant use cases.
– akarnokd
Nov 22 '18 at 15:00
Please provide the definition of
mPublishSubject.– akarnokd
Nov 22 '18 at 13:10
Please provide the definition of
mPublishSubject.– akarnokd
Nov 22 '18 at 13:10
@akarnokd I've updated the question
– Umer Farooq
Nov 22 '18 at 13:15
@akarnokd I've updated the question
– Umer Farooq
Nov 22 '18 at 13:15
Try
PublishSubject<BaseEvent> without the ? extends. Java's generics sometimes doesn't allow certain co- and contravariant use cases.– akarnokd
Nov 22 '18 at 15:00
Try
PublishSubject<BaseEvent> without the ? extends. Java's generics sometimes doesn't allow certain co- and contravariant use cases.– akarnokd
Nov 22 '18 at 15:00
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431731%2frxjava-subject-unable-to-resolve-generic-consumer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431731%2frxjava-subject-unable-to-resolve-generic-consumer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please provide the definition of
mPublishSubject.– akarnokd
Nov 22 '18 at 13:10
@akarnokd I've updated the question
– Umer Farooq
Nov 22 '18 at 13:15
Try
PublishSubject<BaseEvent>without the? extends. Java's generics sometimes doesn't allow certain co- and contravariant use cases.– akarnokd
Nov 22 '18 at 15:00