samber / lo

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)
https://pkg.go.dev/github.com/samber/lo
MIT License
17.71k stars 818 forks source link

Add `CoalesceOrEmptySlice` for slices #513

Open takanuva15 opened 2 months ago

takanuva15 commented 2 months ago

Hi, the CoalesceOrEmpty function is awesome, but it doesn't work for slices. Can we add a counterpart for CoalesceOrEmpty that works for nil/empty slices?

We could call it CoalesceOrEmptySlice and define it with standard generic types like the existing Coalesce:

func CoalesceOrEmptySlice[T ~[]E, E any](v ...T) (result T, ok bool) {

(Alternatively we could try removing the comparable limitation on the existing Coalesce, but I haven't tested whether that would work for nil slices)

chg1f commented 1 day ago

542