if(arr) now a warning
via Digitalmars-d
digitalmars-d at puremagic.com
Fri Apr 10 10:39:23 PDT 2015
On Friday, 10 April 2015 at 10:09:18 UTC, jkpl wrote:
> Then, imagine that the syntactic shortcut is accepted, this
> would lead to the following confusion:
> ---
> char[] * arr;
> if(arr)
> ---
> Here the test is really about the array being null or not.
`arr` is a pointer, not an array. I don't understand what you're
arguing here.
>
> It's like in the topic from yesterday:
>
> ---
> void a(uint p, string a = null){}
> ---
>
> Totally wrong, the guy should write:
>
> ---
> void a(uint p, string a = ""){}
> ---
>
I don't know what that topic was about, but there is nothing
obviously wrong with either version. The language mostly treats
both the same way:
string nullstr = null;
assert(nullstr == "");
assert(nullstr.length == 0);
The only difference is that "" has a non-null address:
assert(!nullstr.ptr);
assert("".ptr);
OTOH, maybe the programmer in question actually wants to make a
distinction between the two, and there's nothing wrong with doing
that either.
> because if you pass a struct as param, the param is never null !
So what? An array isn't a struct.
>
> I'm against the shortcut...it doesn't help to understand how it
> works. It's a useless and confusing shortcut. Warning about the
> amibuous usage was the right
> to do.
I don't think it's confusing. Also note that the shortcut (=>
length) is much more likely to be correct. There are lots of
non-null empty arrays, but I don't know a non-contrived way to
accidentally end up with a non-empty null array.
More information about the Digitalmars-d
mailing list