empty arrays and cast(bool): WAT

Timon Gehr timon.gehr at gmx.ch
Sat Feb 18 19:33:14 PST 2012

On 02/19/2012 04:17 AM, Jonathan M Davis wrote:
> On Sunday, February 19, 2012 04:13:02 Timon Gehr wrote:
>> On 02/19/2012 03:24 AM, bearophile wrote:
>>> Timon Gehr:
>>>> Is there any reason for empty arrays to evaluate to true? This is very
>>>> bug prone.
>>> Related?
>>> http://d.puremagic.com/issues/show_bug.cgi?id=4733
>>> Bye,
>>> bearophile
>> Roughly. I want it to be allowed, but to test the length instead of ptr.
> That would potentially break code.

It would also potentially fix code.

> It also makes arrays less consistent with
> other types that can be null,

If so, then in a good way.

> but the weirdness with [] == null already does
> that on some level.

If you think that is weird, consider this:

static assert([1,2,3] == [1,2,3]);

> My general take on it is that you should just be aware that you need to use
> arr.empty when you mean empty and arr !is null when you mean null. So,
> if(arr)
> is the kind of code that should just be avoided with arrays.

Why? (step back a moment and stop thinking about what you know about 
DMDs implementation semantics and then answer)

More information about the Digitalmars-d mailing list