2009/12/8 Ian Lance Taylor <
ia...@google.com>:
> > it should be straightforward to accomplish this,
> > by adding one bit of information to the Type
> > structure - is the type a pointer?
>
> Unfortunately it's a little more complicated than that. A slice can
> be nil, but a slice is not a pointer.
yes, and the proposal dealt with that. the one bit of information
is actually "is the type nillable?"
> As you know, there are two cases here, and the question is which one
> should be easier. For most types, == nil is true exactly for the
> uninitialized value. You are suggesting that we change that for
> interface types-- == nil will still be true for the uninitialized
> value, but it will also be true for some initialized values. It could
> be done, but I'm not sure that the argument in favor is compelling
> yet.
i agree that it would be a special case, but i think it's justified.
(actually maybe not, see below)
the way i've been thinking of it is that (x == nil)
is just like an method on x, say x.IsNil().
if all nillable types implemented the Nillable interface:
type Nillable interface {
IsNil() bool;
};
then anywhere we wanted to testv against nil,
we could do: v.(Nillable).IsNil()
but making built-in types have methods goes against
the Go grain, so we can't do that.
but it would very useful to be able to generically test against nil
without finding out the exact type. see, for example,
walkIdent, walkCommentGroup, and walkBlockStmt inside
http://golang.org/src/pkg/go/ast/walk.go
all of those functions could be factored out with such
a capability.
actually, thinking about it, maybe just a built-in function isnil
(null?) would do
the job fine. it would work on any interface type
and do the comparison of the contained value against nil
as in my original post.
it would still be easy to trip over the same bug, but at least there
would be an idiomatic way around it.