Skip to content

reflect: document that DeepEqual distinguishes nil and empty slice #4133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
crawshaw opened this issue Sep 23, 2012 · 9 comments
Closed

reflect: document that DeepEqual distinguishes nil and empty slice #4133

crawshaw opened this issue Sep 23, 2012 · 9 comments
Milestone

Comments

@crawshaw
Copy link
Member

http://play.golang.org/p/jUb48jZTJc

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var x []int
    y := []int{}
    fmt.Println(reflect.DeepEqual(x, y))
}

Prints false.

This is not obvious given the documentation, "will scan members of arrays, slices,
maps", and leads to helper functions for unit tests that don't make the distinction.
@minux
Copy link
Member

minux commented Sep 23, 2012

Comment 1:

a nil slice is a SliceHeader where all fields are zero, and an empty slice is a
SliceHeader
where only Len and Cap fields are zero (the Data pointer is non-zero, in fact, we even
allocate space for it now).
I think they are definitely different (an example in C/C++:
char *p = NULL;
char *q = "";
)

@crawshaw
Copy link
Member Author

Comment 2:

They have different memory representations, but DeepEqual is implementing some sort of
structural equality for slices. Consider
    x := []int{1, 3, 1, 3}
    fmt.Println(reflect.DeepEqual(x[:2], x[2:]))
which prints true.
I do not see how distinguishing nil is expected (or useful) here.

@minux
Copy link
Member

minux commented Sep 23, 2012

Comment 3:

but IMO a nil slice doesn't even have a structure to compare with.
as this is the behavior of Go 1's DeepEqual, we can't change that behavior, all
we can do is to document that.

@rsc
Copy link
Contributor

rsc commented Sep 23, 2012

Comment 4:

I don't think we can change it but we should document the nil handling
explicitly. It does throw people.

@robpike
Copy link
Contributor

robpike commented Sep 26, 2012

Comment 5:

Adding documentation, but leaving open in case we want to fix it later.

Labels changed: added go1.1maybe.

@robpike
Copy link
Contributor

robpike commented Sep 26, 2012

Comment 6:

Status changed to Accepted.

@robpike
Copy link
Contributor

robpike commented Sep 26, 2012

Comment 7:

Labels changed: added priority-later, removed priority-triage.

@rsc
Copy link
Contributor

rsc commented Oct 5, 2012

Comment 8:

I think this is done.

@dsymonds
Copy link
Contributor

dsymonds commented Oct 8, 2012

Comment 9:

This was fixed by bb1ad0db059a and 99bb8c9e067d.

Owner changed to @robpike.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1maybe label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned robpike Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants