-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
2.x Remove Function from transformer interfaces to allow a single obj… #4672
2.x Remove Function from transformer interfaces to allow a single obj… #4672
Conversation
…ect to implement multiple transformers
public interface FlowableTransformer<Upstream, Downstream> extends Function<Flowable<Upstream>, Publisher<? extends Downstream>> { | ||
|
||
public interface FlowableTransformer<Upstream, Downstream> { | ||
Publisher<? extends Downstream> apply(Flowable<Upstream> flowable); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid covariant return types (drop ? extends
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was there before in the code I changed, so I left it there. Whether it should be there or not is orthogonal to this request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also an opportunity to fix the signatures, saves 1 PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not certain whether the ? extends should be there or not. I know that I have long had generic type problems with compose and RxLifecycle and am not sure whether the fault is RxJava or RxLifecycle. And the results vary between Java7, Java 8, and Kotlin regarding this.
See this discussion in RxLifecycle: trello-archive/RxLifecycle#58
@@ -1974,7 +1974,12 @@ public final T blockingGet(T defaultValue) { | |||
*/ | |||
@SchedulerSupport(SchedulerSupport.NONE) | |||
public final <R> Maybe<R> compose(MaybeTransformer<T, R> transformer) { | |||
return wrap(to(transformer)); | |||
try { | |||
return wrap(transformer.apply(this)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to try-catch it if it doesn't declare throws Exception
. So either each transformer should specify apply(...) throws Exception
or these are not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does throw exception see later commit
public interface FlowableTransformer<Upstream, Downstream> extends Function<Flowable<Upstream>, Publisher<? extends Downstream>> { | ||
|
||
public interface FlowableTransformer<Upstream, Downstream> { | ||
Publisher<? extends Downstream> apply(Flowable<Upstream> flowable) throws Exception; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? extends
should be removed as covariant return types are usually inconvenience to the consumer.
/cc @JakeWharton |
Another cleanup is that some of the compose methods use paramater name transformer and some use composer, should settle on one or the other. |
Current coverage is 81.99% (diff: 100%)@@ 2.x #4672 diff @@
==========================================
Files 565 565
Lines 37399 37414 +15
Methods 0 0
Messages 0 0
Branches 5746 5746
==========================================
- Hits 30694 30677 -17
- Misses 4614 4648 +34
+ Partials 2091 2089 -2
|
What's the reasoning for the exception in the signatures? |
The reasoning for the exception was that the previous version where they implemented Function supported throwing exceptions so I wasn't trying to change the method signature (there was actually a test for it on one of the types which is why the first build failed). For the more general reasoning for why all the functional interfaces in RxJava2 declare throws Exception see this section: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#functional-interfaces |
Cool, thanks for the explanation! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM pending resolution of the covariant return type comment
Should not a unit test be added to guarantee this behavior? |
It seems I have to do that myself. |
How do you guys feel if all of the operators (CompletableOperator, FlowableOperator, ...) would change to this behavior too for consistency and for the same reasons @dalewking already stated. |
They can be standalone, just don't forget the javadoc. |
I am also thinking about this for the "to" method by changing the XxxxxTransformer interfaces to be more general purpose and usable by both compose and to. Here is what I am suggesting as applied to Observable and if it sounds good to you I will do a PR: The new ObservableTransformer:
compose and to methods:
|
yes |
After having tried implementing what I showed above and seeing how many changes that required in the unit test, I am not sure it is the best approach. It may be better to introduce interfaces like |
This pull request removes Function from the super type of the various Transformer interfaces. While these are technically functions, the problem is that if they all extend Function then you cannot have a single object instance that can implement multiple Transformer interfaces. The goal is to be able to call a function that returns an object that can be passed to Observable.compose or Single.Compose and so on.
This was an issue with RxLifecycle project. See this for more info: trello-archive/RxLifecycle#39