Is empty array null?

Mikael Lindsten mikael at lindsten.net
Tue Feb 28 03:25:20 PST 2012


2012/2/28 Jesse Phillips <jessekphillips+D at gmail.com>
>
>
> string is an array, alias immutable(char)[], so the same rules apply.
>

I know about string being an alias, hence the question. This means that I
can do    if (someString.length) { ... }    without worrying about the null
case (?). That's great!


> There is no NullPointerException, it is a segfault/Access Violation/Bus
> Error.
>
>
Ah, that will take some getting used to. =)
I guess there's a good reason though (there seems to be good reasoning
behind most of the design choises). Off topic, but may I ask what might be
the reason?

An empty array and a null array are equal. And this definition does work
> well.
>
> You can distinguish between an empty array and a null array by using the
> 'is' operator. What is pointed out here is that an empty array literal, [],
> is null while an empty array is not. This behavior is easy to explain in
> terms sf why, but may not be a good choice for sake of consistency.
>

Yes, that feels kind of "wrong" to me - that an empty array literal is null
while an empty array isn't. It's not intuitive, easily explainable or not.


/ Mikael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120228/0cab0624/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list