-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: operator mapIO which is like map but allows checked exceptions #3499
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
Conversation
Why |
The operator naming is up for discussion, I suggested |
Perhaps By the way in v2 have we considered designing Incidentally for Java 8 purposes I use a utility class I call source.doOnNext(Checked.a1(s -> os.write(s.getBytes()))) Though |
In current v2, I'm using as much Java 8 as possible and unfortunately, Java 8 doesn't have standard functional interfaces that specify throws. I know that I've been also thinking about a interface MapActions<T, R, E extends Exception> {
T input();
void output(R r);
void error(E e);
void skip();
void stop();
}
public <R, E extends Exception> Observable<R> mapAction(
Action1E<MapActions<T, R, E>, E> mapper); |
} | ||
}; | ||
|
||
final Func1E<Integer, Integer, Exception> TRHOW_CHECKED = new Func1E<Integer, Integer, 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.
spelling
I'm not to fond of a more functions on rx.Observable, operator implementation code, and test for implementation. I guess I would be ok with @davidmoten |
I would also prefer the changes in #1305. Cursory tests of the solution in @abersnaze's pull request show that it's simple to use either a checked interface or unchecked. public interface Action1Checked<T1> extends Action {
public void call(T1 t1) throws Exception;
}
public interface Action1<T1> extends Action1Checked<T1> {
public void call(T1 t1);
}
public static void main(String[] args) {
bar(o->{});
foo(o->{throw new Exception();});
}
public static void foo(Action1Checked<Object> a) {
try {
a.call(new Object());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void bar(Action1<Object> a) {
a.call(new Object());
} @benjchristensen @headinthebox @akarnokd how should this operate in 2.x? a) support throwing checked exceptions I think we should support this case first class instead of fight against the language feature. We can't force users to write funcs and actions where all exceptions are appropriately caught and handled so the code to handle any runtime exception must be written and maintained. |
Maybe this would just confuse people using 1.x so let's stick to utility wrappers (or perhaps sneaky throwers?). As for 2.x, |
I'm afraid that if we will start adding exception-tolerant versions of operators we will end up 1.5-2xing amount of operators in RxJava 1.x. @akarnokd, others: what if we will make all or as much as possible operators exception-tolerant in RxJava 2.x and leave 1.x as is? |
I'm closing this and we can revisit the problem in 2.x later on. |
It comes up quite frequently that one wants to map values and throw exceptions from within the same function (for example, when dealing with blocking IO that may throw
IOException
). It can be inconvenient to use try-catch and gift-wrap the checked exception into aRuntimeException
.This PR adds a
Func1E
functional interface which has a generic exception parameter as well and the operatormapIO
that accepts it. Changing the existingmap
is out of question because of binary compatibility.