[:] as empty associative array literal, plus warning for null
bearophile
bearophileHUGS at lycos.com
Wed Jul 3 11:10:40 PDT 2013
TommiT:
> But I would expect [] to mean "empty dynamic array", not null.
> And the same for [:]. This is how I'd expect things to work:
>
> int[] nullArray;
> assert(nullArray is null);
>
> int[] emptyArray = [];
> assert(emptyArray !is null);
>
> int[string] nullAA;
> assert(nullAA is null);
>
> int[string] emptyAA = [:];
> assert(emptyAA !is null);
>
> Reasoning by extrapolation:
> int[] arr = [1, 2, 3]; // Array of 3 ints
> int[] arr = [1, 2]; // Array of 2 ints
> int[] arr = [1]; // Array of 1 ints
> int[] arr = []; // Array of 0 ints (not null)
I have discussed the topic here regarding dynamic arrays:
http://d.puremagic.com/issues/show_bug.cgi?id=3889
http://d.puremagic.com/issues/show_bug.cgi?id=5788
This means you have to keep "null" in the language to represent
an empty associative array, because someone somewhere will surely
want a literal that avoids memory allocations. So lot of people
will keep using null, and the coding situation is improved about
zero.
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);
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list