-
Notifications
You must be signed in to change notification settings - Fork 0
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 "must" functions for tersely asserting function success #2
Conversation
// Must0 is similar to Must but for functions returning just an error, without a | ||
// value. | ||
func Must0(err error) func(T) { |
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 function wouldn't have to be curried since a unary function call can be passed to another function directly even when there are other parameters. However, I opted to follow the pattern of the other functions for consistency.
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.
Definitely took me a minute to really understand why we're returning an unary function of T. But now I get it. I kinda long for a type-safe arg spread operator. this is a reasonably un-awkward solution.
// Must3 is similar to Must but for functions returning three values and an | ||
// error. | ||
func Must3[V1 any, V2 any, V3 any](value1 V1, value2 V2, value3 V3, err error) func(T) (V1, V2, V3) { |
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 had to draw the line somewhere, and I stopped at three return values (plus the error). Anything over one or maybe two non-error return values is arguably already excessive.
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.
Seems reasonable - though we could go-generate MustN
if we had to
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.
:noice: - I see my strategy of annoying folks with my hacks until they standardize them works
// Must0 is similar to Must but for functions returning just an error, without a | ||
// value. | ||
func Must0(err error) func(T) { |
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.
Definitely took me a minute to really understand why we're returning an unary function of T. But now I get it. I kinda long for a type-safe arg spread operator. this is a reasonably un-awkward solution.
Dependencies
None.
Documentation
Inspired by test utilities such as this one in a
go-common
branch.Description
This adds a family of four new functions:
Must0
,Must
,Must2
, andMust3
(named in the spirit of theiter
types). These functions take some number of values and an error, and return a function that when called with a*testing.T
either returns the values or fails immediately, if the error is non-nil
. Currying the*testing.T
argument allows functions with multiple return values to be invoked directly within theMust*
call, without assigning them to intermediate values first. The_must
function these are based on panics, but I think failing usingFatalf
can produce nicer error messages.Versioning
Minor. Note that this contains a small breaking change since I added a method (
Fatalf
) to theexpect.T
interface. However,expect
is still v0, and looking at howexpect.T
is used in our code, there shouldn't be any breakage anyway.