On Wed, Jan 18, 2023 at 6:28 PM 'Christian Stewart' via golang-nuts
<golang-nuts@googlegroups.com> wrote:
>
> Thing is an interface in the first example I gave:
>
> If you want to compare two interfaces:
>
> var foo1 Thing
> var foo2 Thing
>
> Ordinarily you can do foo1 == foo2 and it does pointer-wise comparison.

It's true that if you store pointer values in a variable of interface
type that it does pointer-wise comparisons.  But you can store values
of any type in a variable of interface type, not just pointer values.

https://gotipplay.golang.org/p/3swFWinr8Pb

> But in a generic function, as an example:
>
> // IsEmpty checks if the interface is equal to nil.
> func IsEmpty[T Block](blk T) bool {
>     var empty T
>     return empty == blk
> }
>
> (I had blk Block before, instead of blk T).
>
> Comparing with empty is something I've needed to do a bunch of times
> and have been unable to do.

The type argument to a function like IsEmpty is rarely an interface
type.  So the comparison in the instantiation IsEmpty is not comparing
values of interface type.  It's comparing values of whatever type
IsEmpty is instantiated with.  And it is possible to instantiate
IsEmpty with types that can't be compared, even with their zero value,
such as [1][]byte.

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/CAOyqgcWc69k0iWG8Y_LEAFg29wxP%2BXtc0876ek_WtrS4_AXKqA%40mail.gmail.com.

Reply via email to