-
-
Notifications
You must be signed in to change notification settings - Fork 825
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
Conversation
fe91b7f
to
4053043
Compare
@@ -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 { |
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 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 …
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 is a reasonable suggestion, maybe I can adjust must
along with it.
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.
How do you feel about changing err
to failure
?
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.
Naming is a pain.
@samber is better than I am 😬😂
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 have a good friend named ChatGPT. Perfect for brainstorming.
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?
|
Thanks for your review. I really like short and strong names like |
Hi @samber |
Hi @samber, |
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 |
Summary
This PR introduces a new helper function named
EmptyOnError
along withEmptyOnError1
toEmptyOnError6
. 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. TheEmptyOnError
helper and its variants (EmptyOnError1
toEmptyOnError6
) 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 noEmptyOnError0
variant, as it would be redundant.