Why is array truth tied to .ptr?

Bruce Adams tortoise_74 at yeah.who.co.uk
Mon Dec 10 02:14:40 PST 2007


On Mon, 10 Dec 2007 09:07:13 -0000, Bill Baxter  
<dnewsgroup at billbaxter.com> wrote:

> Janice Caron wrote:
>> On Dec 10, 2007 2:01 AM, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>>> BTW, if someone does feel the current behavior is more rational or
>>> functional in some way, then I'd love to hear it.  Perhaps it's my
>>> familiarity with Python, but I just don't get why you'd want if(array)
>>> to mean anything other than if(array.length).
>>  Personally, I'd prefer if(array) to fail to compile, because I don't
>> like the idea of implicit conversions to bool. That would force you to
>> say what you mean - i.e. either
>>      if (array.length == 0)
>>  or
>>      if (array is null)
>
> I also would prefer an error to the current behavior of doing something  
> unintuitive that looks like it works correctly but in fact does  
> something slightly different.
>
I suspect you are moving into the realms of lint here. I would say it  
should not
even be a warning in the core language. In linted variant you are
free to enforce stricter rules. I thought I saw a link to a dlint sometime
ago.
>
>> I suspect the current behaviour really means
>>      if (array.ptr == null && array.length == 0)
>
> I guess you are free to look at it it that way, however if you look at  
> the truth table I posted in the original message, the array.length == 0  
> is redundant there.  if array.ptr is null length is always zero.
>
I think her point was that its not redundant for the case of array.ptr != 0
as in:

    foo[5] x; //not initialised
    if (x)
    {
      ...
    }

>> which is not entirely unreasonable. Certainly, in the case of strings
>> (as special type of array) the current behaviour allows one to
>> distinguish between an unassigned string and an empty string - which
>> is occasionally desirable.
>
> If the default were changed to check array.length you could still  
> achieve that by explicitly checking "if(array.ptr)"
>
True. The default should be what is most useful in practice.



More information about the Digitalmars-d mailing list