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

Add EmptyOnError, EmptyOnError{1->6} #495

Closed
wants to merge 4 commits into from

Conversation

yuweiweiouo
Copy link

@yuweiweiouo yuweiweiouo commented Jul 16, 2024

Summary

This PR introduces a new helper function named EmptyOnError along with EmptyOnError1 to EmptyOnError6. These functions are designed to safely retrieve values while ignoring errors, ensuring that in the case of an error, empty values are returned instead of triggering a panic.

Motivation

In many scenarios, developers may only be interested in retrieving a value and are willing to ignore potential errors without risking a panic. The existing Must helper function, while useful, panics when an error is encountered, which might not always be desirable. The EmptyOnError helper and its variants (EmptyOnError1 to EmptyOnError6) provide a safer alternative by returning empty values in case of an error.

Unlike Must, which can handle zero return values, EmptyOnError focuses solely on the values returned by a function. Therefore, there is no EmptyOnError0 variant, as it would be redundant.

@yuweiweiouo yuweiweiouo force-pushed the master branch 4 times, most recently from fe91b7f to 4053043 Compare July 16, 2024 08:38
@@ -113,6 +113,76 @@ func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5
return val1, val2, val3, val4, val5, val6
}

// just assume fine if err is nil or true.
func fine(err any) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't use err as a variable name here, as the type is unknown.

I would update the signature of all Just* methods

But it's the way must was written, so …

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a reasonable suggestion, maybe I can adjust must along with it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about changing err to failure?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is a pain.

@samber is better than I am 😬😂

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a good friend named ChatGPT. Perfect for brainstorming.

@samber
Copy link
Owner

samber commented Jul 16, 2024

Hi @yuweiweiouo and thanks for your first contribution.

About the naming, At first, I thought that "Just" was referring to the reactive programming helper that creates a stream of N elements.

Can you suggest other names?

  • EmptyOnError
  • other?

@yuweiweiouo
Copy link
Author

yuweiweiouo commented Jul 16, 2024

Hi @yuweiweiouo and thanks for your first contribution.

About the naming, At first, I thought that "Just" was referring to the reactive programming helper that creates a stream of N elements.

Can you suggest other names?

  • EmptyOnError
  • other?

Thanks for your review. I really like short and strong names like Must.

@yuweiweiouo yuweiweiouo changed the title Add Just, Just{1->6} Add Fine, Fine{1->6} Jul 17, 2024
@yuweiweiouo
Copy link
Author

Hi @samber
I've gone with Fine
WDYT?

@yuweiweiouo yuweiweiouo requested a review from samber July 22, 2024 01:47
@yuweiweiouo yuweiweiouo changed the title Add Fine, Fine{1->6} Add EmptyOnError, EmptyOnError{1->6} Aug 26, 2024
@yuweiweiouo
Copy link
Author

Hi @samber,
I've changed it to the EmptyOnError, Could you kindly review it?

@ccoVeille
Copy link
Contributor

Was it merged or abandoned?

@yuweiweiouo
Copy link
Author

Was it merged or abandoned?

Since it has been idle for too long and there has been no response, I believe this PR will not be accepted, so I closed it for now

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.

3 participants