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

TommiT tommitissari at hotmail.com
Thu Jul 4 10:58:28 PDT 2013


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. So, the problem is that [] is not what 
you'd intuitively expect it to be.

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.


More information about the Digitalmars-d mailing list