Skip to content

Add ifSuccessOrElse method to Try for unified handling of success or failure cases#36

Merged
SuppieRK merged 4 commits intoSuppieRK:masterfrom
vesav-mms:master
Sep 5, 2025
Merged

Add ifSuccessOrElse method to Try for unified handling of success or failure cases#36
SuppieRK merged 4 commits intoSuppieRK:masterfrom
vesav-mms:master

Conversation

@vesav-mms
Copy link
Copy Markdown
Contributor

No description provided.

@SuppieRK
Copy link
Copy Markdown
Owner

SuppieRK commented Sep 4, 2025

Hey @vesav-mms! Thanks for the PR.

I think I understand the intent behind the change, and I believe that it goes against the functional API approach.

Can you provide your opinion as to why this change should be merged with an example of the use case you are trying to achieve?

@vesav-mms
Copy link
Copy Markdown
Contributor Author

Intention is to be able to do following operations that otherwise do not look straight forward that are possible:

 Try.of(...)
        .ifSuccess(....)
        .ifFailure(...);

Basically, I want to turn operations like the following one that is not that intuitive in the construct above.

 var try = Try.of(...);
 try.ifSuccess(....);
 try.ifFailure(...);

@SuppieRK
Copy link
Copy Markdown
Owner

SuppieRK commented Sep 4, 2025

@vesav-mms as I suspected.

Instead of implementing these operations like you propose, how about we implement method:

void ifSuccessOrElse(ThrowableConsumer<? super T> valueConsumer, ThrowableConsumer<Throwable> throwableConsumer)

, which would follow the same logic as ifPresentOrElse​ method from Java 9 Optional?

My reason to avoid having the ability to chain for ifSuccess and ifFailure is to keep the semantic close to Java Optional, as well as preventing scenarios which will force these methods to act like a peek:

Try.of(...)
        .ifSuccess(....)
        .ifFailure(...)
        .map(...)
        .filter(...)
        .ifSuccess(....)
        .ifFailure(...)

@vesav-mms vesav-mms changed the title Add chained operation support for ifSuccess and ifFailure in Try Add ifSuccessOrElse method to Try for unified handling of success or failure cases Sep 5, 2025
@vesav-mms
Copy link
Copy Markdown
Contributor Author

@SuppieRK changes done!

@SuppieRK SuppieRK merged commit d747a05 into SuppieRK:master Sep 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants