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

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Jul 4 08:58:08 PDT 2013


On 7/4/13 8:53 AM, Andrei Alexandrescu wrote:
> On 7/4/13 8:37 AM, monarch_dodra wrote:
>> On Thursday, 4 July 2013 at 15:27:17 UTC, Timon Gehr wrote:
>>> On 07/04/2013 04:35 PM, Andrei Alexandrescu wrote:
>>>> On 7/4/13 6:32 AM, Steven Schveighoffer wrote:
>>>>> I would not be opposed to a pull request that made [] be non-null, as
>>>>> long as it doesn't allocate.
>>>>
>>>> What would be the benefits?
>>>>
>>>> Andrei
>>>
>>> - Additional sentinel values at basically no cost.
>>>
>>> - No accidental flawed relying on empty array is null or empty array
>>> !is null.
>>> (i.e. less nondeterminism.)
>>>
>>> - One thing less to discuss (this has come up before.)
>>
>> There are no benefits to making "[]" return null either. Implementation
>> wise, instead of returning a void[] with "ptr == 0x0" and "length == 0",
>> it could just as well return a void[] with "ptr == 0x1" and "length ==
>> 0".
>>
>> You'd get better behavior at no extra cost.
>
> I'm clear on the no extra cost part, but confused about the benefits.

BTW you get the no extra cost part with a sheer function:

auto emptyArray(T)() @trusted
{
     return (cast(T*) 1)[0 .. 0];
}


Andrei


More information about the Digitalmars-d mailing list