is ==

Jonathan M Davis newsgroup.d at
Sun May 20 00:19:28 UTC 2018

On Saturday, May 19, 2018 17:50:50 IntegratedDimensions via Digitalmars-d-
learn wrote:
> So, ultimately what I feels like is that you are actually arguing
> for == null to be interpreted as is null but you don't realize it
> yet.

Not really, no. Having

foo == null

be rewritten to

foo is null

in the non-dynamic array cases should be fine except for the fact that it's
then a terrible habit to be in when you then have to deal with dynamic
arrays. Using

foo == null

with dyanmic arrays is an enormous code smell, because the odds are extemely
high that the programmer thinks that they're checking if the dynamic array
is null when that's not what they're doing at all. IMHO, it should
definitely be an error to use == with null and dynamic arrays because it is
such a big code smell. Either the code should be using is to check whether
the array is null, or it should be checking length. It should never be using
== with null.

But unfortunately, the compiler is completely backwards about this and
treats it as an error with pointers and references but allows it with
dynamic arrays. If the compiler were improved to just replace == with is in
the cases that it currently treats as illegal, then that would be fine if it
then treated it as illegal with dynamic arrays. But as it stands, it is
still more efficient to use is with call references, so encouraging the
programmer to use is is benefical, and it encourages the programmer to get
in the habit of not using == with null, since it's a terrible habit to be in
with dynamic arrays. But actually making it illegal for dynamic arrays would
be a much better approach.

If it were up to me, it would just be illgal to use == with null in general,
because that's really the way it should be with dynamic arrays, and then the
language would be consistent about it. But instead, the compiler screams in
the case that matters far less and allows it in the case that is clearly
bad. So, it's inconsistent in a dumb way. At least if it were inconsistent
by allowing it for pointers and references while disallowing it for arrays,
it would be prventing it in the case that truly matters, but instead, what
we have is just dumb.

- Jonathan M Davis

More information about the Digitalmars-d-learn mailing list