Empty VS null array?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Oct 18 10:38:12 PDT 2013


On Fri, Oct 18, 2013 at 01:32:58PM -0400, Jonathan M Davis wrote:
> On Friday, October 18, 2013 09:55:46 Andrei Alexandrescu wrote:
> > On 10/18/13 9:26 AM, Max Samukha wrote:
> > > *That's* bad API design. readln should be symmetrical to writeln,
> > > not write. And about preserving the exact representation of new
> > > lines, readln/writeln shouldn't preserve that, pure and simple.
> > 
> > Fair point. I just gave one possible alternative out of many. Thing
> > is, relying on client code to distinguish subtleties between empty
> > and null strings is fraught with dangers.
> 
> Yeah, but the primary reason that it's bad design is the fact that D
> tries to conflate null and empty instead of keeping them distinct
> (which is essentially the complaint that was made). Whether that's
> ultimately good or bad is up for debate, but the side effect is that
> relying on the difference between null and empty ends up being very
> bug-prone, whereas in other languages which don't conflate the two, it
> isn't problematic in the same way, and it's much more reasonable to
> have the API treat them differently.
[...]

IMO, distinguishing between null and empty arrays is bad abstraction.  I
agree with D's "conflation" of null with empty, actually. Conceptually
speaking, an array is a sequence of values of non-negative length. An
array with non-zero length contains at least one element, and is
therefore non-empty, whereas an array with zero length is empty. Same
thing goes with a slice. A slice is a view into zero or more array
elements. A slice with zero length is empty, and a slice with non-zero
length contains at least one element. There's nowhere in this conceptual
scheme for such a thing as a "null array" that's distinct from an empty
array. This distinction only crops up in implementation, and IMO leads
to code smells because code should be operating based on the conceptual
behaviour of arrays rather than on the implementation details.


T

-- 
The most powerful one-line C program: #include "/dev/tty" -- IOCCC


More information about the Digitalmars-d mailing list