This syntax regarding null checking baffles me
12345swordy
alexanderheistermann at gmail.com
Thu Jan 7 14:13:04 UTC 2021
On Thursday, 7 January 2021 at 05:14:44 UTC, Basile B. wrote:
> On Thursday, 7 January 2021 at 04:57:55 UTC, 12345swordy wrote:
>> if (c !is null) Why?????
>>
>> Would it be simpler to type
>>
>> if (c is not null)
>>
>> on a related note. Why are not we allowed to do this?
>>
>> if (c != null)
>
> it is allowed
>
"For class objects, the == and != operators are intended to
compare the contents of the objects, however an appropriate
opEquals override must be defined for this to work. The default
opEquals provided by the root Object class is equivalent to the
is operator. Comparing against null is invalid, as null has no
contents. Use the is and !is operators instead."
Not allowed for classes apparently.
-Alex
More information about the Digitalmars-d
mailing list