Behaviour of AAs after initialization

Puming via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 7 20:10:14 PDT 2014


On Thursday, 7 August 2014 at 18:05:15 UTC, H. S. Teoh via 
Digitalmars-d wrote:
> On Thu, Aug 07, 2014 at 05:42:28PM +0000, via Digitalmars-d 
> wrote:
>> On Thursday, 7 August 2014 at 17:35:46 UTC, Puming wrote:
>> >So I'd like to suggest a rule here similar to what assignment 
>> >does to
>> >null AA:
>> >
>> >If someone refers to an uninitialized null AA ( in 
>> >implementation,
>> >that maybe, a copy of a null AA struct happens), allocate it 
>> >first.
>> 
>> I'm afraid that copying is too general. This would trigger on 
>> just
>> about any access. It would also make copying AAs more 
>> expensive. I
>> believe a standardized method for initializing an AA is more 
>> likely to
>> be accepted.
>
> It really just needs a standard function in druntime that does 
> this.
> Perhaps something like this:
>
> 	// in object.di
> 	T initialize(T : V[K], V, K)(T aa = null) {
> 		aa[K.init] = V.init;
> 		aa.remove(K.init);
> 		// or if you like, encapsulate this in aaA.d and just
> 		// allocate Impl without the add/delete hackery.
>
> 		return aa;
> 	}
>
> 	// in user code
> 	auto aa = initialize!(string[int]);
>
> The name / syntax is up for bikeshedding, but the idea is 
> pretty simple.
> Make a druntime function that allocates the initial empty AA, 
> and make
> that function accessible to user code.
>
>
> T

That is what I'm using now...
But the implementation (assignement plus remove) would look weird.

People were asking for

```
string[int] aa = [];
// or
string[int] aa = [:];
```

is it hard to implement, or confict with other language features?

But if we have a initialize mechanism, it would be better to 
deprecate the 'silent allocate on first assignment', because 
people would easily forgot to init the aa, and then refer to it, 
and the compiler being silent on this, only finding that the 
original one is not affected later, contrary to their intentions.

If we want: "You must initialize an AA before refering to it", 
the compiler should enforce that.




More information about the Digitalmars-d mailing list