"" gives an empty string, while "".idup gives null

Mike Parker aldacron at gmail.com
Wed Aug 3 08:27:12 PDT 2011


On 8/3/2011 11:23 PM, simendsjo wrote:
> On 03.08.2011 15:49, bearophile wrote:
>> simendsjo:
>>
>>> void main() {
>>> assert(is(typeof("") == typeof("".idup))); // both is immutable(char)[]
>>>
>>> assert("" !is null);
>>> assert("".idup !is null); // fails - s is null. Why?
>>> }
>>
>> I think someone has even suggested to statically forbid "is null" on
>> strings :-)
>>
>> Bye,
>> bearophile
>
> How should I test for null if not with "is null"? There is a difference
> between null and empty, and avoiding this is not necessarily easy or
> even wanted.
> I couldn't find anything in the specification stating this difference.
> So... Is it a bug?
>

This is apparently a bug. Somehow, the idup is clobbering the pointer. 
You can see it more clearly here:

void main()
{
	assert("".ptr);
	
	auto s = "".idup;
	assert(s.ptr); // boom!
}


More information about the Digitalmars-d-learn mailing list