null == "" is true?

user1234 user1234 at 12.de
Tue Jul 12 20:35:27 UTC 2022


On Tuesday, 12 July 2022 at 19:55:46 UTC, ag0aep6g wrote:
> On Tuesday, 12 July 2022 at 19:02:01 UTC, user1234 wrote:
>> On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote:
> [...]
>>> Do not rely on this, however;
>>
>> Absolutely. I'd like to add: especially as default parameter 
>> value that's an array. Never use null. use `[]` (empty array 
>> literal).
>
> Just to be clear: `[]` and `null` are the exact same thing 
> (null pointer, zero length). The reason to prefer `[]` over 
> `null` is purely for readability. The meaning is exactly the 
> same.

ah yes. The case I thought to was actually

```d
void test1(string s = null)
{
     assert(s is null);
}

void test2(string s = "") // s is null term'ed, i.e not null
{
     assert(s is null);
}

void main()
{
     test1();
     test2(); // fails
}
```

the rule of thumb is to use `stuff.length` as condition, always, 
and not `stuff` itself, to prevent natyx, hard to find bugs.


More information about the Digitalmars-d-learn mailing list