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

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 27 09:56:21 PDT 2016


On 5/27/16 11:49 AM, ArturG wrote:
> On Friday, 27 May 2016 at 15:24:18 UTC, Adam D. Ruppe wrote:
>> On Friday, 27 May 2016 at 15:19:50 UTC, ArturG wrote:
>>> yes but i have to check for that when some one does
>>
>> Why? This is no different than if they set any of the other four
>> billion possible values.
>
> What do you mean?
>
> operation on float.nan gives you a float.nan so why does the shortcut
> evaluate to true and not false wouldnt that make more sense?
>
>
> float f;
> if(f) "why does this print".writeln; // it should not
>
> same for char
>
> char c;
> if(c) "why does this print".writeln; // it should not
>
> it works for most other types is there any reason why it doesnt work or
> couldnt work with floating points and character types?

conversion to bool is not universally (val !is val.init)

Why are you expecting it to be?

Won't work for enums with first elements that are non-zero either:

enum foo : int {
  bar = 1;
}

foo f;

if(f) writeln("this will output too");

-Steve


More information about the Digitalmars-d-learn mailing list