reflect.DeepEqual vs bytes.Equal

976 views
Skip to first unread message

Ondrej Kupka

unread,
Aug 24, 2013, 10:52:56 AM8/24/13
to golan...@googlegroups.com
Hi,

Is there any reason why

reflect.DeepEqual([]byte(""), []byte(nil)) -> false

while

bytes.Bytes([]byte(""), []byte(nil)) -> true

? You can also check it here: http://play.golang.org/p/yAH9oFWChr .

This is quite inconsistent I must say, I've just spent quite some time before I realized this…

Regards,
Ondrej Kupka

atkaaz

unread,
Aug 24, 2013, 11:24:37 AM8/24/13
to Ondrej Kupka, golan...@googlegroups.com



--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Brad Fitzpatrick

unread,
Aug 24, 2013, 3:05:45 PM8/24/13
to Ondrej Kupka, golang-nuts
These are both documented.

reflect.DeepEqual: "An empty slice is not equal to a nil slice."
bytes.Equal: "A nil argument is equivalent to an empty slice."

In the types of places you use each, this is what you almost always want.



Ondřej Kupka

unread,
Aug 25, 2013, 4:16:13 AM8/25/13
to golan...@googlegroups.com, Ondrej Kupka
Hi,

On Saturday, August 24, 2013 9:05:45 PM UTC+2, bradfitz wrote:
These are both documented.


Yes, I've noticed, a bit too late, but whatever :-)
 
reflect.DeepEqual: "An empty slice is not equal to a nil slice."
bytes.Equal: "A nil argument is equivalent to an empty slice."

In the types of places you use each, this is what you almost always want.


I was obviously expecting DeepEqual to work as recursive bytes.Equal
in case I want to compare two [][]byte. And I was like what the hell?
They have the same content, but then I had it printed by each []byte
and [] != [], so they are not equal with DeepEqual, because
[]byte(nil) != []byte("") :-)

So I guess I have to do it myself []byte by []byte...

Regards,
Ondrej Kupka

roger peppe

unread,
Aug 25, 2013, 10:53:05 AM8/25/13
to Brad Fitzpatrick, Ondrej Kupka, golang-nuts
On 24 August 2013 20:05, Brad Fitzpatrick <brad...@golang.org> wrote:
> These are both documented.
>
> reflect.DeepEqual: "An empty slice is not equal to a nil slice."
>
> bytes.Equal: "A nil argument is equivalent to an empty slice."
>
> In the types of places you use each, this is what you almost always want.

I've found that the DeepEqual behaviour is almost never what
I want - it's awkward when testing and you have to depend
on the subtle difference between:

var x []string
for ... {
x = append(x, foo)
}

and

x := []string{}
for ... {
x = append(x, foo)
}

I've been thinking of making a version of reflect.DeepEqual that
compares a nil slice equal to an empty slice, just to get around
that awkwardness.

To be honest, I also wish that a nil slice marshalled as json [] not null
for similar reasons.

Of course, we can't change either of these now...

Aram Hăvărneanu

unread,
Aug 25, 2013, 11:04:19 AM8/25/13
to roger peppe, Brad Fitzpatrick, Ondrej Kupka, golang-nuts
> I've found that the DeepEqual behaviour is almost never what
> I want - it's awkward when testing and you have to depend
> on the subtle difference between: [ snip ]

Code in Juju made use of this subtle distinction (not sure if it's
still the case). I always found it unfortunate.

--
Aram Hăvărneanu
Reply all
Reply to author
Forward
0 new messages