Thread overview
Re: Initializing assoc array to non-null
Jun 01, 2013
Jonathan M Davis
Jun 01, 2013
d coder
Jun 01, 2013
Jonathan M Davis
June 01, 2013
On Saturday, June 01, 2013 20:21:42 d coder wrote:
> Greetings
> 
> Is there a way to initialize an associative array to a non-null (but still empty) state? The only way I know is by adding an element and then removing it. Did I miss something obvious? Basically I want to write lines 7-8 in the following code in a cleaner fashion. Any ideas?

Unfortunately, at the moment, I believe that that's the best that you can do - though it's trivial enough to write a function that does that for you so that you can make it a one-liner:

auto aa = initAA!(string[int])();

or

initAA(aa);

- Jonathan M Davis
June 01, 2013
Thanks Jonathan

Do you think this could make a good enhancement request?

Regards
- Puneet



On Sat, Jun 1, 2013 at 9:34 PM, Jonathan M Davis <jmdavisProg@gmx.com>wrote:

> On Saturday, June 01, 2013 20:21:42 d coder wrote:
> > Greetings
> >
> > Is there a way to initialize an associative array to a non-null (but
> still
> > empty) state? The only way I know is by adding an element and then
> removing
> > it. Did I miss something obvious? Basically I want to write lines 7-8 in the following code in a cleaner fashion. Any ideas?
>
> Unfortunately, at the moment, I believe that that's the best that you can
> do -
> though it's trivial enough to write a function that does that for you so
> that
> you can make it a one-liner:
>
> auto aa = initAA!(string[int])();
>
> or
>
> initAA(aa);
>
> - Jonathan M Davis
>


June 01, 2013
On Saturday, June 01, 2013 21:45:26 d coder wrote:
> Thanks Jonathan
> 
> Do you think this could make a good enhancement request?

Yes. There are some key things like that in AAs that should be improved. There's also no way to clear an AA. There are people who call clear (which has now be renamed to destroy), but that just nulls the reference. To truly clear it, you have to actually manually remove all of the elements, which isn't exactly ideal.

I know that Don has argued that AAs shouldn't be null by default, but that changing that might break existing code in subtle ways and wouldn't play nice with CTFE as it currently stands. We do need a better way to handle it though.

- Jonathan M Davis