On Tue, Oct 3, 2023 at 12:06 PM Jon Watte <jwa...@gmail.com> wrote: > > I don't want to include int or struct{} in this case. I care specifically > about "can be compared to nil" > The only thing I can do with a type parameter that is only constrained as > "nil" is compare it to nil, or assign nil to it. > This means approximately "any reference type" -- interfaces, pointers, maps, > slices, chans...
It's not obvious that that is an interesting set of types. How often do people want to write a generic function that permits any type that can be compared to nil but does not permit any numeric or string type? > But, let's put this out there: > How would you, today, write a function that compacted a slice of interface, > such that any "nil" interface value would be removed from the slice? Unless I misunderstand, you seem to have shifted the goal a bit. There is currently no way to restrict a type argument to be an interface type, with or without your suggestion of a nil constraint. > func Compact[T ?](sl []T) []T { > j := 0 > for i := 0; i < len(sl); i++ { > if el := sl[i]; el != nil { > sl[j] = el > j++ > } > } > return sl[:j] > } > > There's nothing today I can put in place of the "?" to make this > perfectly-reasonable generic function work. > I propose I should be able to put "nil" in place of the "?" to make this > work, and it would work for any reference type (that can be compared or > assigned to nil.) > > Unless I'm missing something? Well, here is a version that works for a slice of any type, including an interface type. Arguably, though, using reflect is cheating. func Compact[T any](s []T) []T { j := 0 for _, v := range s { vv := reflect.ValueOf(v) if vv.IsValid() && !vv.IsZero() { s[j] = v j++ } } return s[:j] } Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUaTdPE5-FGqqBRtr5kf-kM3b4rfsvwyFUBYu75gVZ8Ow%40mail.gmail.com.