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

Single valued return type for TryOr #380

Open
nettnikl opened this issue Sep 11, 2023 · 1 comment
Open

Single valued return type for TryOr #380

nettnikl opened this issue Sep 11, 2023 · 1 comment

Comments

@nettnikl
Copy link

Hi!

Suppose the following:

filepath := filepath.Join(folder, lo.TryOr(config.GetTargetFile, "default.txt"))

we receive a Multiple-value lo.TryOr(...) (string, bool) in single-value context error.

How to get rid of the (in most of my use cases unneeded) boolean?
I'd like to not implement additional functions, if possible, but definitely not much longer code (i.e. putting it in an extra line to value, _ = it. My example could survive that readability wise, my real code sadly not.
I would expect something like Must, but for the bool instead of an error, and ofc without panics.

@nettnikl
Copy link
Author

For reference, current workaround:

func TryOr[A any](callback func() (A, error), fallbackA A) A {
	val, _ := lo.TryOr1(callback, fallbackA)
	return val
}

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

No branches or pull requests

1 participant