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

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Jul 4 11:03:01 PDT 2013


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.

> 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.

> 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 [].


Andrei


More information about the Digitalmars-d mailing list