> It seems to me `== nil` is a better way to spell that, than a function call. And a generic function can always do `v == *new(T)` to check if a value is the zero value.
Bringing this up because I found it from a thread on Google. I want to implement a generic function to "remove nil values from a slice." This should be able to remove nil instances of error, for example. So, let's say I have: var arg = []error{nil, errors.New("oh noes"), nil} Unfortunately, this doesn't work: func Compact[T comparable](s []T) T { ... if s[i] == nil { because "error" is not comparable. This also doesn't work: func Compact[T any](s []T) T { var zero T ... if s[i] == zero { because T is not a comparable constraint. It may be that "error" is somehow special, and is "the only thing" that doesn't fit neatly into "zero values" or "comparables," but it's a pretty basic fundament of the runtime, so having to special-case everything about it is quite frustrating and cumbersome. Sincerely Jon Watte On Thursday, December 22, 2022 at 5:39:29 AM UTC-8 Axel Wagner wrote: > On Thu, Dec 22, 2022 at 2:23 PM Pierre Durand <pierre...@gmail.com> wrote: > >> Is there a way to declare a "nillable" constraint that matches all types >> that can be nil (pointer, slice, map, etc.) ? > > > There is currently no way to do that. In particular, any interface type > can be `nil`, therefore any such constraint would have to be satisfied by > all interface types. > > >> I would like to write a function, that receives a parameter (with a >> generic type), and check if this parameter is nil. >> > > I don't understand the point, to be honest. It seems to me `== nil` is a > better way to spell that, than a function call. And a generic function can > always do `v == *new(T)` to check if a value is the zero value. > > >> I found a very ugly way to write it: https://go.dev/play/p/0g0SoTlBEgs >> The problem: the map type needs more than 1 type, so I need to provide >> the 3 types when I call the `IsNil()` function/ >> Is there a better way ? >> >> Yes I know I could use `any`, and check with reflect if the value inside >> the interface is nil. >> But I want to know if it's possible to do it with generics. >> >> Thank you. >> >> -- >> 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...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/2043afe5-3d18-445a-90a9-75b48d3ec078n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/2043afe5-3d18-445a-90a9-75b48d3ec078n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- 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/01f1280f-029e-4b37-ae7e-324a62279fedn%40googlegroups.com.