Just a friendly reminder about using arrays in boolean conditions
user1234
user1234 at 12.de
Mon Nov 18 12:24:01 UTC 2024
On Monday, 18 November 2024 at 12:20:17 UTC, user1234 wrote:
> On Sunday, 17 November 2024 at 08:57:35 UTC, Jonathan M Davis
> wrote:
>> [...]
>
> Well I agree, even if that will take years to have .length
> tested instead of .ptr.
>
> Little story however: I've encountered a case where the explict
> check was also wrong.
>
> ```d
> module runnable;
>
> import std.stdio;
>
> void v(string s)
> {
> if (s.length) writeln("case length :`", s, "`");
> else if (s is null) writeln("case null :`", s, "`");
> else writeln("case not null but no
> length:`", s, "`");
> }
>
> void main(string[] args)
> {
> v("hello");
> v(null);
> v("");
> }
> ```
>
> The different semantics between `null` and `""` for strings is
> well illustrated here I'd say.
I promised a litlle sotry, here it is:
https://gitlab.com/basile.b/harbored-mod/-/commit/5ec14dfe98f91384c1b50e7c50fb4767a297d06f
quite the exact same problem, just here the author tested against
null explictly.
More information about the Digitalmars-d
mailing list