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

lo.Ternary crash with varags #617

Open
moneyease opened this issue Apr 3, 2025 · 1 comment
Open

lo.Ternary crash with varags #617

moneyease opened this issue Apr 3, 2025 · 1 comment

Comments

@moneyease
Copy link

Test code

https://go.dev/play/p/LIx60Wu_tT9

As expected it should work.

@CosmicPredator
Copy link

Since you are passing a[0] as parameter, It gets evaluated at runtime even though the condition is false. One fix is to not use ternary operator and directly use if { } else { } or you can use lo.TernaryF like this,

func foo(a ...string) string {
    predicate := len(a) == 1
    return lo.TernaryF(predicate, func() string { return a[0] }, func() string { return "" })
}

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

2 participants