-
Notifications
You must be signed in to change notification settings - Fork 65
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
Introduces throwIf and throwUnless #66
Conversation
Having written this, I'm not convinced that it's actually all that ergonomic to use. Having to call it with two type parameters, despite the There's also the fact that if you call it without specifying the type parameters, it does nothing and gives you no indication that it will do nothing. More detail in the kdoc comment would help that, but still makes it a bit unintuitive. |
I agree regarding the necessity to explicitly type it, and the lack of effect when failing to do so is troublesome and will definitely catch people out. Wonder what other approaches we can think of. |
Two possible alternatives I was thinking about:
|
@Munzey the problem with both of those approaches is the same as my original problem: any function taking E.g. runThrowingCatching<String, CancellationException> {... or whatever value and exception types you wanted. It's the fact that you have to specify |
@michaelbull I'll submit a PR for |
Actually, I've just remembered another potential solution to this I'd thought of. I'll just sketch it here: fun <V> Result<V, Throwable>.rethrowIf(
predicate: (Throwable) -> Boolean
): Result<V, Throwable> It's more general than Then we could write runSuspendCatching( block: suspend () ->V) =
runCatching(block)
.rethrowIf { it is CancellableException } |
could we avoid the noise of adding the exception to the generic params by just passing the kclass as parameter? runCatchingRethrow(exceptionClazz: KClass<*>) And just comparing |
Another thought I just had regarding the semantics of |
|
I like the I also think the comments made earlier about "throw" vs "rethrow" are worth considering. If we were never going to throw then "rethrow" doesn't make sense, so something like |
257f08c
to
48112c1
Compare
48112c1
to
b07a57d
Compare
Finally got some time to work on this. I've changed 'rethrow' to 'throwIf' and implemented 'throwUnless' with tests for both. |
3810567
to
b26cd5f
Compare
Introduces the
rethrow
function as discussed in #64.