Forbid dynamic arrays in boolean evaluation contexts

Vladimir Panteleev vladimir at thecybershadow.net
Mon Mar 25 19:21:47 PDT 2013


On Tuesday, 26 March 2013 at 01:57:10 UTC, Steven Schveighoffer 
wrote:
> On Mon, 25 Mar 2013 17:28:51 -0400, Vladimir Panteleev 
> <vladimir at thecybershadow.net> wrote:
>
>> On Monday, 25 March 2013 at 14:46:27 UTC, Steven Schveighoffer 
>> wrote:
>>> I would favor just changing the behavior.
>>
>> That would silently break my code.
>
> It would seem incomplete not to have if(arr) work, and the way 
> it works now is very error prone.
>
> You would have to change your code either way.  Deprecating 
> than reintroducing seems gratuitous to me.  Most people do not 
> use if(arr) to check for array pointer when if(arr.ptr) is more 
> descriptive.
>
> How much do you use this "feature"?  Can they simply be 
> replaced with if(arr.ptr)? or are they more of the type Jacob 
> has with if(auto a = getArray())?  I'm trying to get a feel for 
> how much breakage this causes, as I typically do not use that 
> construct.

No, it's nothing like that. In fact, it's very simple:

Users will download my open-source program, compile it 
successfully, then try using it - at which point, it will crash, 
produce incorrect output, or do something equally bad. Would you 
not agree that this is unacceptable?

Furthermore, I would have no way to automatically find all places 
where I would need to change my code. I would need to look at 
every if statement of my program and see if its behavior depends 
on whether the string/array is empty or null. My program is quite 
large, so, again, this is unacceptable.

I use this feature in the same way that anyone uses a nullable 
type. It's the same distinction between a pointer to struct that 
is null, or that is pointing to an instance containing the 
struct's .init. It's the same distinction between a value type T 
and the benefits of Nullable!T. "null" is usually used to 
indicate the absence of a value, as opposed to an empty value.

Although I can see how this can trip up new users of D, 
personally I prefer things to be just the way they are now. That 
said, I wouldn't be against forcing one to write "s is null", if 
the consensus was that this would improve D.


More information about the Digitalmars-d mailing list