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

Timon Gehr timon.gehr at gmx.ch
Thu Jul 4 11:31:04 PDT 2013


On 07/04/2013 08:03 PM, Andrei Alexandrescu wrote:
> On 7/4/13 10:58 AM, TommiT wrote:
>> On Thursday, 4 July 2013 at 17:32:29 UTC, Andrei Alexandrescu wrote:
>>> [..] Maybe someone returned [] thinking it will be a null array. [..]
>>
>> I wouldn't think that [] is null, and I suspect neither would very many
>> other newcomers to the language. To me, the only problem with [] being
>> null is that it doesn't look like null. It looks like an empty array.
>
> A null array _is_ an empty array.
>

Whether null is [] is the subject of this discussion.

There are multiple possibilities, such as:

1. null !is [], but [] is []
2. null !is [], and not even [] is []
3. !is(typeof(null is [])) // also, !is(typeof(null == []))
4. null is []

1 is the most useful, 2 is the most wasteful, 3 is the sanest, and 4 is 
what DMD/druntime implement.

The spec is loose enough to allow an implementation to behave like 1, 2 
or 4 or in even other ways.


>> So, the problem is that [] is not what you'd intuitively expect it to be.
>
> Is the intuition that it's an empty array that's not part of anything,
> presumably obtained by forging 1 into a pointer? Didn't think so.
>

Why do multiple persons come up with this suggestion independently?

>> By the way, this must be a bug, right?
>>
>> template arr(X_...)
>> {
>> int[] arr = [X_]; // [1]
>> }
>>
>> void main()
>> {
>>     auto a2 = arr!(1, 2);
>>     auto a1 = arr!(1);
>>     auto a0 = arr!(); // [2]
>> }
>>
>> [1] Error: initializer must be an expression, not '()'
>> [2] Error: template instance main.arr!() error instantiating
>>
>> ...because if that's not supposed to work, then I don't see much point
>> in having the [] literal in the language.
>
> I also happen to think we don't quite need [].
>

[1,2,3]
[1,2]
null
[1]
[]

Find Waldo.


More information about the Digitalmars-d mailing list