[:] as empty associative array literal, plus warning for null

Steven Schveighoffer schveiguy at yahoo.com
Thu Jul 4 06:32:29 PDT 2013


On Thu, 04 Jul 2013 08:52:12 -0400, Regan Heath <regan at netmail.co.nz>  
wrote:

> On Thu, 04 Jul 2013 12:50:54 +0100, Steven Schveighoffer  
> <schveiguy at yahoo.com> wrote:
>
>> On Thu, 04 Jul 2013 05:25:30 -0400, Regan Heath <regan at netmail.co.nz>  
>> wrote:
>>
>>> On Wed, 03 Jul 2013 19:10:40 +0100, bearophile  
>>> <bearophileHUGS at lycos.com> wrote:
>>>> Telling apart the literal for an empty array from the literal of a  
>>>> empty but not null array is a bad idea that muds the language. And  
>>>> thankfully this currently fails:
>>>>
>>>> void main() {
>>>>      int[] emptyArray = [];
>>>>      assert(emptyArray !is null);
>>>> }
>>>
>>> As this comes up often you're probably aware that there are people  
>>> (like myself) who find the distinction between a null (non-existant)  
>>> array and an empty array useful.
>>
>> Nobody questions that.
>> The biggest problem is making if(arr) mean if(arr.ptr) instead of  
>> if(arr.length)
>
> Indeed.  IMO if(arr) should mean if(arr.ptr) .. and I thought it did..  
> or did this change at some point?

No, it should mean if(arr.length).  It means if(arr.ptr) now, and this is  
incorrect.  I don't care if an empty array points at null or at some other  
value, I care if it's empty or not.  You can always use if(arr is null) or  
if(arr.ptr) if you prefer to distinguish null arrays from non-null ones.   
As of now, it's impossible to not care.

>
>> What [] returns should not be an allocation. And returning null is a  
>> reasonable implementation of that.
>
> Whether there is an allocation or not is secondary.  The primary goal is  
> for [] to represent empty, not null.  We have null, if we want to  
> represent null we pass null.  What we lack is a way to represent empty.

What I'm saying is that [] returning null is reasonable.  Returning some  
non-null empty array is also reasonable if it's not an allocation.

In the realm of arrays, you are not supposed to care where it's stored,  
you only care about the elements and the length.

I would not be opposed to a pull request that made [] be non-null, as long  
as it doesn't allocate.

-Steve


More information about the Digitalmars-d mailing list