Associative Array c'tor
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jul 12 05:34:40 PDT 2016
On 7/12/16 5:42 AM, ketmar wrote:
> On Tuesday, 12 July 2016 at 03:38:44 UTC, Bahman Movaqar wrote:
>> Now I understand.
>> This is tricky --could introduce hard to find bugs. Is there anyway to
>> make sure it doesn't happen? Such as giving the AA a default empty
>> value on the declaration line --like `string[int] a = []`?
>
> no. the only thing you can do is to add something to aa and immediately
> `.clear`
There was a suggestion to make .clear (a relatively new feature)
actually preallocate if it's currently null, but I didn't want to do
allocating in that method (too surprising). I do think it would be nice
to have an initializer function that simply allocates the impl.
Anyone want to do a PR?
basically (strawman name):
int[int] aa;
aa.initialize; // still has 0 elements, but no longer null.
I'm not sure you could make a static initializer method, since AA's are
special builtin types.
Relative modules needed to be changed are object.d and src/rt/aaA.d
-Steve
More information about the Digitalmars-d-learn
mailing list