if(arr) now a warning
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Fri Apr 10 11:38:16 PDT 2015
On 4/10/15 1:39 PM, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm at gmx.net>"
wrote:
> 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.
I know of one place that has a non-empty null array, TypeInfo.init():
import std.stdio;
struct S
{
int x;
}
void main()
{
auto x = typeid(S).init();
writeln(x.ptr, " ", x.length);
}
output:
null 4
The reason for this is, the compiler is telling you the size is 4 bytes,
but they should all be 0 (no need to store 0 bytes in data segment).
But this is NOT a usual situation.
-Steve
More information about the Digitalmars-d
mailing list