weird empty string

mogu via Digitalmars-d digitalmars-d at puremagic.com
Fri May 12 20:41:29 PDT 2017


On Saturday, 13 May 2017 at 00:59:14 UTC, Stanislav Blinov wrote:
> On Saturday, 13 May 2017 at 00:36:55 UTC, mogu wrote:
>> ```d
>> if (null)
>>     "1".writeln;
>> if ("")
>>     "2".writeln;
>> if ("" == null)
>>     "3".writeln;
>> ```
>>
>> Output:
>> 2
>> 3
>>
>> How to understand this?
>
> Boolean conversion on an array works on array's pointer, not 
> it's length. So even though `"".length == 0`, `"".ptr != null`, 
> and so `cast(bool)"" == true`.

```d
string s1 = null;
string s2 = "";
assert(s1 == null);
assert(s1.ptr == null);
assert(s2 == null);
assert(s2.ptr != null);
if (s1)
     1.writeln;
if (s2)
     2.writeln;
```

Output:
2

Thanks very much. This is a little bit confusing.


More information about the Digitalmars-d mailing list