View mode: basic / threaded / horizontal-split · Log in · Help
May 14, 2012
arrays: if(null == [ ])
Hi! I have a small question:
Is the test for a null array equivalent to a test for zero-length array?
This is particularly interesting for strings.
For instance, I could return an empty string from a toString-like function
and the empty string would be printed, but If I returned a null string,
that would indicate, that there is no string representation and it would
cause some default string to be printed.
So, the question is, if a null array is any different from an empty array?

-- 
Bye,
Gor Gyolchanyan.
May 14, 2012
Re: arrays: if(null == [ ])
On Mon, 14 May 2012 12:08:17 +0200, Gor Gyolchanyan  
<gor.f.gyolchanyan@gmail.com> wrote:

> Hi! I have a small question:
> Is the test for a null array equivalent to a test for zero-length array?
> This is particularly interesting for strings.
> For instance, I could return an empty string from a toString-like  
> function
> and the empty string would be printed, but If I returned a null string,
> that would indicate, that there is no string representation and it would
> cause some default string to be printed.
> So, the question is, if a null array is any different from an empty  
> array?
>

This passes. null and [] is a null string, but "" gives a non-null string.  
Tested on dmd-head and 2.059.

void main() {
    string s1 = null;
    assert(s1 is null);
    assert(s1.length == 0);
    assert(s1.ptr is null);
    assert(s1 == []);
    assert(s1 == "");

    string s2 = [];
    assert(s2 is null);
    assert(s2.length == 0);
    assert(s2.ptr is null);
    assert(s2 == []);
    assert(s2 == "");

    string s3 = "";
    assert(s3 !is null);
    assert(s3.length == 0);
    assert(s3.ptr !is null);
    assert(s3 == []);
    assert(s3 == "");
}
May 14, 2012
Re: arrays: if(null == [ ])
On Mon, 14 May 2012 12:08:17 +0200, Gor Gyolchanyan  
<gor.f.gyolchanyan@gmail.com> wrote:

> Hi! I have a small question:
> Is the test for a null array equivalent to a test for zero-length array?
> This is particularly interesting for strings.
> For instance, I could return an empty string from a toString-like  
> function
> and the empty string would be printed, but If I returned a null string,
> that would indicate, that there is no string representation and it would
> cause some default string to be printed.
> So, the question is, if a null array is any different from an empty  
> array?
>

The two are different, yes. [] == null actually compares length. If you
want to know if the length is zero, use arr.length. If you want to know if
it points to null, check if arr.ptr is null.
May 14, 2012
The more interesting question
On 05/14/12 12:08, Gor Gyolchanyan wrote:
> Hi! I have a small question:
> Is the test for a null array equivalent to a test for zero-length array?
> This is particularly interesting for strings.
> For instance, I could return an empty string from a toString-like function and the empty string would be printed, but If I returned a null string, that would indicate, that there is no string representation and it would cause some default string to be printed.
> So, the question is, if a null array is any different from an empty array?
> 
> -- 
> Bye,
> Gor Gyolchanyan.

The more interesting question, imo, is how the behavior of 'if (string)' (ie. bool conversion) should be defined. To my knowledge,
it checks for ptr, which can lead to some confusion since "" == null, 'if ("")' is true but 'if (null)' is false!

I think this behavior is still best though, for the simple reason that people think (or ought to think) of arrays as "pointers
with length", so it makes sense that 'if (string)' tests the pointer. It's not intuitively obvious if you consider strings as sequences
of characters, but it's obvious if you consider them as D arrays.
May 14, 2012
Re: arrays: if(null == [ ])
So, null arrays and empty arrays are always the same, except for an empty
string, which is a valid non-nill array of characters with length 0, right?

On Mon, May 14, 2012 at 2:24 PM, simendsjo <simendsjo@gmail.com> wrote:

> On Mon, 14 May 2012 12:08:17 +0200, Gor Gyolchanyan <
> gor.f.gyolchanyan@gmail.com> wrote:
>
>  Hi! I have a small question:
>> Is the test for a null array equivalent to a test for zero-length array?
>> This is particularly interesting for strings.
>> For instance, I could return an empty string from a toString-like function
>> and the empty string would be printed, but If I returned a null string,
>> that would indicate, that there is no string representation and it would
>> cause some default string to be printed.
>> So, the question is, if a null array is any different from an empty array?
>>
>>
> This passes. null and [] is a null string, but "" gives a non-null string.
> Tested on dmd-head and 2.059.
>
> void main() {
>    string s1 = null;
>    assert(s1 is null);
>    assert(s1.length == 0);
>    assert(s1.ptr is null);
>    assert(s1 == []);
>    assert(s1 == "");
>
>    string s2 = [];
>    assert(s2 is null);
>    assert(s2.length == 0);
>    assert(s2.ptr is null);
>    assert(s2 == []);
>    assert(s2 == "");
>
>    string s3 = "";
>    assert(s3 !is null);
>    assert(s3.length == 0);
>    assert(s3.ptr !is null);
>    assert(s3 == []);
>    assert(s3 == "");
> }
>



-- 
Bye,
Gor Gyolchanyan.
May 14, 2012
Re: arrays: if(null == [ ])
Le 14/05/2012 12:49, Gor Gyolchanyan a écrit :
> So, null arrays and empty arrays are always the same, except for an
> empty string, which is a valid non-nill array of characters with length
> 0, right?
>

If it is the current behavior, it deserve a WAT !
May 14, 2012
Re: The more interesting question
Le 14/05/2012 12:42, FeepingCreature a écrit :
> On 05/14/12 12:08, Gor Gyolchanyan wrote:
>> Hi! I have a small question:
>> Is the test for a null array equivalent to a test for zero-length array?
>> This is particularly interesting for strings.
>> For instance, I could return an empty string from a toString-like function and the empty string would be printed, but If I returned a null string, that would indicate, that there is no string representation and it would cause some default string to be printed.
>> So, the question is, if a null array is any different from an empty array?
>>
>> --
>> Bye,
>> Gor Gyolchanyan.
>
> The more interesting question, imo, is how the behavior of 'if (string)' (ie. bool conversion) should be defined. To my knowledge,
> it checks for ptr, which can lead to some confusion since "" == null, 'if ("")' is true but 'if (null)' is false!
>
> I think this behavior is still best though, for the simple reason that people think (or ought to think) of arrays as "pointers
> with length", so it makes sense that 'if (string)' tests the pointer. It's not intuitively obvious if you consider strings as sequences
> of characters, but it's obvious if you consider them as D arrays.

A good solution would be to set the pointer to 0 when the length is set 
to 0.
May 14, 2012
Re: arrays: if(null == [ ])
On Mon, 14 May 2012 13:51:40 +0200, deadalnix <deadalnix@gmail.com> wrote:

> Le 14/05/2012 12:49, Gor Gyolchanyan a écrit :
>> So, null arrays and empty arrays are always the same, except for an
>> empty string, which is a valid non-nill array of characters with length
>> 0, right?
>>
>
> If it is the current behavior, it deserve a WAT !

It is according to my tests.. It's quite a gotcha.
So check for .length or == "" or == [] if you need "null or empty" and use  
"is null" for null/[]
May 14, 2012
Re: arrays: if(null == [ ])
I think any kind of null array should be different from an array of zero
length.

On Mon, May 14, 2012 at 3:55 PM, simendsjo <simendsjo@gmail.com> wrote:

> On Mon, 14 May 2012 13:51:40 +0200, deadalnix <deadalnix@gmail.com> wrote:
>
>  Le 14/05/2012 12:49, Gor Gyolchanyan a écrit :
>>
>>> So, null arrays and empty arrays are always the same, except for an
>>> empty string, which is a valid non-nill array of characters with length
>>> 0, right?
>>>
>>>
>> If it is the current behavior, it deserve a WAT !
>>
>
> It is according to my tests.. It's quite a gotcha.
> So check for .length or == "" or == [] if you need "null or empty" and use
> "is null" for null/[]
>



-- 
Bye,
Gor Gyolchanyan.
May 14, 2012
Re: The more interesting question
deadalnix , dans le message (digitalmars.D:167258), a écrit :
> A good solution would be to set the pointer to 0 when the length is set 
> to 0.

String literal are zero-terminated. "" cannot point to 0x0, 
unless we drop this rule. Maybe we should...
« First   ‹ Prev
1 2 3 4 5
Top | Discussion index | About this forum | D home