On 8/20/15 12:45 PM, "=?UTF-8?B?Ik3DoXJjaW8=?= Martins\" <[email protected]>\"" wrote:
Hi!

string a = "";
string b;

writeln(a ? "a" : "null");
writeln(b ? "b" : "null");


This program outputs:
a
null



What?

I suppose this is by design, but are there any plans to deprecate this?

The "truthiness" of an array says it's true ONLY if both the pointer and length are 0.

Yes, by design, and no, it won't be deprecated. In fact it was deprecated, but was reverted by the language designer as too costly in terms of code breakage.

The main reason why it caused issues is this nice idiom:

if(auto arr = someFunction())
{
   // use arr
}

This would HAVE to be split out to two statements, and the arr variable would be scoped outside of the if statement.

Having 2 empty strings evaluate differently is very unintuitive and
error-prone, in my opinion.

Very true. Not much we can do about it. For now, the best thing to do is always compare arrays to null instead of simply if(arr):

if(arr != null)

Alternatively, but a little more ugly, is to check the length:

if(arr.length)

-Steve

Reply via email to