The more interesting question

H. S. Teoh hsteoh at
Wed May 16 11:32:44 PDT 2012

On Wed, May 16, 2012 at 02:03:44PM -0400, Steven Schveighoffer wrote:
> On Wed, 16 May 2012 13:52:57 -0400, H. S. Teoh
> <hsteoh at> wrote:
> >It depends upon one's mental model of what an array is.
> >
> >If you think of an array as a container that exists apart from its
> >contents, then you'd expect null != [] because null means even the
> >container itself doesn't exist, whereas [] means the container exists
> >but contains nothing.
> >
> >However, if you regard the array simply as the sum total of its
> >contents, then you'd expect null == [] because there is no container
> >to speak of, either there are elements, or there are none. When there
> >are no elements, there is also no array (or equivalently, the array
> >is empty). Therefore, null and [] are the same thing.
> >
> >It seems that D takes the latter view, at least as far as == is
> >concerned. Thus, to distinguish between null and [], one has to
> >bypass == and use 'is' (i.e., open up the hood of the mental model of
> >an array, and look into its actual implementation).
> Part of the source of this confusion is that D slices are not actually
> arrays or containers.  They reference, they don't contain.

Right, which is why D's arrays (or rather, slices) are closer to the
second model than the first. Actually, arrays only exist in the GC,
right? Even an "explicitly declared" array is really just a slice, that
just happens to reference the contents of that chunk of GC memory.

Except static arrays, of course, but we're not worried about those here.


Never trust an operating system you don't have source for! -- Martin Schulze

More information about the Digitalmars-d mailing list