ref semantics with hashes

Steven Schveighoffer schveiguy at yahoo.com
Thu May 3 07:15:46 PDT 2012


On Wed, 02 May 2012 16:32:04 -0400, H. S. Teoh <hsteoh at quickfur.ath.cx>  
wrote:

> On Wed, May 02, 2012 at 09:38:35PM +0200, Andrej Mitrovic wrote:
> [...]
>> So if the hash wasn't already initialized then the reference in the
>> Foo struct is a reference to null, and if you duplicate that reference
>> and add a key the old reference still points to null.
>>
>> The only way to ensure a proper link with a hash is to initialize it
>> with a key and then immediately remove that key, which makes the hash
>> not-null but empty:
> [...]
>> Why do we have such error-prone semantics?
>
> I was told that this was expected behaviour.
>
> Should the new AA implementation change this, so that it always
> allocates an empty AA upon instantiation?

This can't be done.  You can always create a struct without calling any  
code.

What I'd like to see is an initializer function/property, e.g.:

auto a = int[int].create(); // static property that allocates a new empty  
AA.

-Steve


More information about the Digitalmars-d-learn mailing list