On Thu, Dec 22, 2022 at 2:23 PM Pierre Durand <pierredur...@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+unsubscr...@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/CAEkBMfH-4aRxrGY2VrfWs52PecpjWR-s0yGOO7m-3%3Dpg37H9BQ%40mail.gmail.com.

Reply via email to