"".dup is null
Steven Schveighoffer
schveiguy at yahoo.com
Mon May 4 07:30:07 PDT 2009
On Mon, 04 May 2009 10:22:49 -0400, Qian Xu <quian.xu at stud.tu-ilmenau.de>
wrote:
> Steven Schveighoffer wrote:
>
>> I think you might have a bug?
>>
>> "".dup is the same as s.dup, not sure why you would expect it to be
>> not-null.
>>
>> -Steve
>
> If I have not explained clearly.
> Here is the full code:
>
> char[] s;
> assert(s is null);
> assert(s.dup is null);
>
> assert("" !is null); // OK
> assert("".dup !is null); // FAILED
>
> At least the last two lines behave not consistent.
> Either both are failed, or both are passed.
OK, your original post was this:
assert( s.dup is null); // OK
assert("".dup !is null); // FAILED
The compiler always returns a null array if you dup an empty array. The
reason being: why allocate memory for something that is zero length?
If anything, the oddity is this line:
assert("" !is null);
But of course, no memory is allocated for literals, so at least needless
memory allocation does not occur.
To be consistent, I think assert("" is null); should pass, but it's a
minor inconsistency at best.
I usually never compare arrays to null for this reason...
-Steve
More information about the Digitalmars-d-learn
mailing list