Jump to page: 1 29  
Page
Thread overview
arrays: if(null == [ ])
May 14, 2012
Gor Gyolchanyan
May 14, 2012
simendsjo
May 14, 2012
Gor Gyolchanyan
May 14, 2012
deadalnix
May 14, 2012
simendsjo
May 14, 2012
Gor Gyolchanyan
May 14, 2012
Timon Gehr
May 16, 2012
bearophile
May 16, 2012
Tobias Pankrath
May 16, 2012
Timon Gehr
May 16, 2012
Jonathan M Davis
May 17, 2012
Regan Heath
May 24, 2012
Marco Leise
May 14, 2012
Simen Kjaeraas
The more interesting question
May 14, 2012
FeepingCreature
May 14, 2012
deadalnix
May 14, 2012
Christophe
May 14, 2012
Gor Gyolchanyan
May 15, 2012
deadalnix
May 15, 2012
Christophe
May 15, 2012
Gor Gyolchanyan
May 15, 2012
Dmitry Olshansky
May 15, 2012
David Nadlinger
May 15, 2012
Gor Gyolchanyan
May 15, 2012
David Nadlinger
May 15, 2012
deadalnix
May 15, 2012
Andrew Wiley
May 15, 2012
deadalnix
May 15, 2012
Gor Gyolchanyan
May 15, 2012
deadalnix
May 16, 2012
Timon Gehr
May 16, 2012
deadalnix
May 16, 2012
H. S. Teoh
May 16, 2012
deadalnix
May 16, 2012
Timon Gehr
May 15, 2012
Timon Gehr
May 15, 2012
deadalnix
May 15, 2012
Timon Gehr
May 15, 2012
deadalnix
May 16, 2012
Gor Gyolchanyan
May 16, 2012
Gor Gyolchanyan
May 16, 2012
Gor Gyolchanyan
May 16, 2012
Gor Gyolchanyan
May 16, 2012
H. S. Teoh
May 16, 2012
H. S. Teoh
May 16, 2012
H. S. Teoh
May 16, 2012
Jonathan M Davis
May 16, 2012
Gor Gyolchanyan
May 16, 2012
H. S. Teoh
May 18, 2012
Christophe Travert
May 18, 2012
Jonathan M Davis
May 22, 2012
Christophe Travert
May 15, 2012
Timon Gehr
May 24, 2012
Marco Leise
May 14, 2012
deadalnix
May 15, 2012
deadalnix
May 16, 2012
deadalnix
May 17, 2012
deadalnix
May 14, 2012
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
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
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
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
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
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
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
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
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
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 6 7 8 9