-
Notifications
You must be signed in to change notification settings - Fork 7.6k
utility function for creating observables for closeable resources #1282
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
RxJava-pull-requests #1182 SUCCESS |
@@ -50,6 +54,73 @@ | |||
public static Observable<byte[]> from(final InputStream i) { | |||
return from(i, 8 * 1024); | |||
} | |||
|
|||
private static class CloseableResource<S extends AutoCloseable> implements Subscription { |
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 won't work with Java 6.
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 guess I'll switch it to Closeable then
RxJava-pull-requests #1186 SUCCESS |
Can someone else involved in Also, this needs to be rebased now as it collides with another change that was merged. |
RxJava-pull-requests #1191 FAILURE |
UnsafeFunc0 now extends Callable.
whoops bad merge |
RxJava-pull-requests #1192 SUCCESS |
@davidmoten can you take a look? |
yep will do |
* @param <R> | ||
*/ | ||
public static interface UnsafeFunc0<R> extends Callable<R> { | ||
public R call() 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.
I might have missed something here. I don't see that the UnsafeFunc0 adds much. After all every Func0 is potentially unsafe inasmuch as it can throw a RuntimeException for instance.
I see your point about the advantage when writing lambdas. Could you equivalently have a utility function that wrapped the lambda to do the wrapping with a RuntimeException as in:
If it works you might get your code brevity back without your changes being necessary. |
ah I just looked up java 8 lambdas and checked exceptions and I see my suggestion has no legs |
Righto so I've tested the handling of lambdas and checked exceptions in java 8 and I can see this is a handy utility. It's a really interesting idea to then extend FuncN from UnsafeFuncN and I can see this would work. The more extreme implementation would be to add My only concern then is really the name because Funcs are inherently unsafe as there is nothing in the contract for a Func that says it can't throw a RuntimeException or Error. The alternative is something yuk like FuncCanThrowCheckedException. |
How about CheckedFunc? I agree that I would like to see CheckedFunc used in core where ever an RuntimeException from a Func is caught and pushed down to onError. The reasoning is that if the implementation is already wrapping calls with a try/catch then let the Func throw whatever it wants. |
Do you see an impediment to changing the signature of FuncN to include throws Exception? |
Adding to FuncN directly would be too much. It forces operator implementations to always wrap exceptions even if they don't care about propagating RuntimeExceptions up the call stack. |
there's the tradeoff I suppose between operator implentations and users using java 8 lambdas with rx operators and catching exceptions. In theory there'd be more of the latter right? |
I'm thinking about |
The map operator explicit does a try catch around the Func1 call so that would be a candidate for switching to CheckedFunc1. |
if we did map() I'd be tempted to do it everywhere. Might have to do an inventory of where Func is used in the Operators |
utility function for creating observables for closeable resources
#1199 the answer to the issue was nagging at me when I tried to recommend it recently. I think @zsxwing's solution was ok but would fail if there were repeated subscriptions to the observable returned. I created a utility function on StringObservable to make it easier to use
using()
for objects that implement theAutoCloseable
interface.PS. I made a breaking change to arguments of
join(Observable<Object>)
tojoin(Observable<String>)
and added aObservable<String> toString(Observable<Object>)
to make up for it.PPS. I made a
UnsafeFunc0<R>
scoped to this class but I would like to haveUnsafeFunc<N>
in the rx.functions packages and haveFunc<N>
extendsUnsafeFunc<N>
. Makes it clear to the compiler that we don't expect the code the user's are giving us to always work. This also helps the users because they don't have to always fill their closures with try catch blocksvs.