There was a bug, and it has been fixed. A nil slice is not the same as
a slice which exists but is empty.
Ian
-rob
> unless the cup is happens to be a string :-)
An empty slice can potentially be grown without allocation. A nil slice can never be grown without allocation.
A string can never be grown at all.
-rob
You're not using the cup. You're asking the amount of stuff in the
cup, which is the same for a missing or an empty cup.
Analogies aside, in practice this is brilliant. Many algorithms can be
simplified when len and cap don't panic on nil slices and maps.
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog
-- I'm not absolutely sure of anything.
You may want to distinguish an uninitialized slice from a slice which
has been used and re-sliced until the end. eg:
package main
import(
"fmt"
"reflect"
)
func main() {
var a, b []int
// use the slice a
a = append(a, 1)
a = a[1:]
fmt.Println(reflect.DeepEqual(a,b))
}
I would find surprising that this program printed "true", as it was
happening. In my opinion it is clear that it was a bug.
The current (fixed) behavior can be useful to differentiate an
uninitialized list (nil: empty cup) from a list which has already been
used (a[len(a):]: I don't know if the cup is empty or full anymore,
but I'm quite sure it is not deeply equal to the missing cup).
--
- yiyus || JGL .