This syntax regarding null checking baffles me

ag0aep6g anonymous at example.com
Sun Jan 10 18:10:08 UTC 2021


On Sunday, 10 January 2021 at 10:42:48 UTC, Jonathan M Davis 
wrote:
> IIRC, if the class overrides opCast for bool, then
>
>     if(c)
>
> will cast the object to bool and use the result for the if 
> condition, whereas
>
>     if(c !is null)
>
> always checks whether the reference is null.

You're misremembering. Or maybe it was changed since you last 
checked. A rewrite from `if (c)` to `if (c.opCast!bool)` "only 
happens, however, for instances of structs. Class references are 
converted to bool by checking to see if the class reference is 
null or not."

https://dlang.org/spec/operatoroverloading.html#boolean_operators


More information about the Digitalmars-d mailing list