Skip to content

Commit

Permalink
builtin: add Must()
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Apr 3, 2023
1 parent 089aa58 commit 6259354
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions builtin/builtin.go
Expand Up @@ -36,6 +36,7 @@ func Pointer[T any](val T) *T {

// helper for wrapping an error with error prefix.
// if error is nil, nil is returned.
// Deprecated: this doesn't carry its own weight - just use `fmt.Errorf()`
func ErrorWrap(prefix string, err error) error {
if err != nil {
return fmt.Errorf("%s: %w", prefix, err)
Expand Down Expand Up @@ -64,6 +65,16 @@ func FirstNonEmpty[T comparable](values ...T) T {
return empty
}

// for a function that returns two values, you can get the value with `Must(fn())` to convert it to
// the value, panicking if producing the value caused an error
func Must[T any](value T, err error) T {
if err != nil {
panic(err)
}

return value
}

// returns first error, or nil if no errors
// Deprecated: use `FirstNonEmpty()`
func FirstError(errs ...error) error {
Expand Down

0 comments on commit 6259354

Please sign in to comment.