Why do some T.init evaluate to true while others to false?

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 26 08:11:50 PDT 2016


On Thursday, 26 May 2016 at 14:03:16 UTC, ArturG wrote:
> for example:
>
> if(any floatingpoint.init) will be true
> if(any char.init) also true
> if("") also true
>
> while others are false e.g.
>
> string s;
> if(s) will be false
> all others are also false or did i miss any?

It's a shortcut that works for certain type and that means:

- pointers: if (ptr)  <=> if (ptr != null)
- pointers: if (!ptr) <=> if (ptr == null)
- integral(*): if (i) <=> if (i > 0)
- integral: if (!i)   <=> if (i == 0)
- classes: if (c)     <=> if (c !is null)
- classes: if (!c)    <=> if (c is null)

(*) integral: generally speaking so: byte, ubyte, short, ushort, 
int, uint, long, ulong, char, wchar, dchar and also, very special 
case, structs with an alias this to one of this integral type.

for array this works and this tests the (.ptr) member but most of 
the people here (incl. me) would recommand rather to always do:

     "if (arr.length)"

because in some cases "if (arr)" will yield "true" even if the 
length is equal to 0.


More information about the Digitalmars-d-learn mailing list