Checking if a string is null
Regan Heath
regan at netmail.co.nz
Thu Jul 26 01:09:40 PDT 2007
Frits van Bommel wrote:
> Or would you prefer a segfault or diagnostic when accessing
> (cast(T[])null).length?
No, definately not. This is one of the things I love about arrays,
they're both value and reference type. It takes a while to get your
head round (if the many discussions on these forums are any indication)
but once you have it worked out it's quite powerful. In fact it's the
reason slicing can work the way it does.
Further, for those cases where we do not care to differentiate between
null and "" checking length == 0 is the perfect solution.
I'm not interested in an array implementation which is 'pure' in any
academic sense but rather one which is consistent in that null arrays do
not become empty and vice-versa under any conditions (other than
explicitly assigning those values).
For example:
In the past setting length to 0 would free the data pointer. The result
of which was that a zero length (empty) array became a non-existant
(null) array.
And the problem we have now is that calling .dup on an empty array
results in a null array.
It is cases like these which I was to remove.
The other thing I want is for == to tell me that null and "" are not the
same.
I suspect very little existing code is relying on the existing behaviour
as it will likely be checking length as opposed to comparing to "" or
null (note; comparing with == not checking identity with "is").
Regan
More information about the Digitalmars-d-learn
mailing list