string <-> null/bool implicit conversion

via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 20 12:04:26 PDT 2015


On Thursday, 20 August 2015 at 18:42:56 UTC, Márcio Martins wrote:
> On Thursday, 20 August 2015 at 18:04:00 UTC, Steven 
> Schveighoffer wrote:
>> On 8/20/15 1:50 PM, Steven Schveighoffer wrote:
>>
>>> The "truthiness" of an array says it's true ONLY if both the 
>>> pointer and
>>> length are 0.
>>
>> Ugh, *false* only if they are both 0. If either are not zero, 
>> then it's true.
>>
>> -Steve
>
> In other words, it's true when the pointer is not null. i.e. in 
> the context of boolean evaluation, it has the semantics of 
> string.ptr
>
> It's just making the concept of an empty array a grey cloud.
>
> Consider this:
>
> string a = "";
> string b;
> 	
> writeln(a ? "a" : "null");
> writeln(b ? "b" : "null");
> writeln(a.idup ? "adup" : "null");
> writeln(b.idup ? "bdup" : "null");
>
> Output:
> a
> null
> null
> null
>
>
> What?

Also, consider this:

writeln(a ? true : false);
writeln(a && a.idup);

Output:
true
false

What?

Should we consider this weird semantics and live with it, as 
there is nothing we can do, or is it a bug in idup()?


More information about the Digitalmars-d mailing list