Skip to content
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

Simplify Reactor switchIfEmpty(Mono.just(...)) using defaultIfEmpty #363

Closed
1 of 3 tasks
EnricSala opened this issue Nov 21, 2022 · 1 comment
Closed
1 of 3 tasks
Labels
Milestone

Comments

@EnricSala
Copy link

EnricSala commented Nov 21, 2022

Problem

Specifying default behavior in a Reactor publisher can be written like switchIfEmpty(Mono.just(42)) or defaultIfEmpty(42).

The behavior is identical, but the latter is more concise and may have slight performance benefits due to reduced overhead.

Description of the proposed new feature

  • Support a stylistic preference.
  • Avoid a common gotcha, or potential problem.

I would like to rewrite the following code:

Mono<Integer> mono = ...;
return mono.switchIfEmpty(Mono.just(42));

to:

Mono<Integer> mono = ...;
return mono.defaultIfEmpty(42);

Considerations

  • The rewrite operation is applicable for both Mono and Flux.
  • This rewrite is only suitable when the default is a {Mono|Flux}.just(...). Cases like Mono.fromSupplier cannot be rewritten because they would lose the lazy-evaluation behavior.
  • It could be possible to rewrite also cases like switchIfEmpty(Mono.empty()), in which case the switchIfEmpty operator can simply be dropped.

Participation

  • I am willing to submit a pull request to implement this improvement.
@giall
Copy link
Contributor

giall commented Nov 25, 2022

Opened a PR: #370. The third point in considerations seems to already be covered by the MonoSwitchIfEmptyOfEmptyPublisher rule.

@rickie rickie closed this as completed in 6d15cfe Nov 28, 2022
@Stephan202 Stephan202 added this to the 0.6.0 milestone Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants