August 09, 2021

On Monday, 9 August 2021 at 13:10:53 UTC, Dukc wrote:

>

...however, if you initialize a with emptyAA.dup it will again be null!

I believe we can only conclude that empty AA's are not supposed to rely on being non-null. Sorry Walter - in my eyes this is a design failure.

I don't think this is a much of a problem, after all, .dup is meant to create a new identity, and you can't really rely on that identity being anything specific.

That being said, yes, there is clearly some level of snafu involved in that design.

August 09, 2021

On Monday, 9 August 2021 at 13:21:06 UTC, jfondren wrote:

>

You get this with dynamic arrays as well.

They're very convenient, scripting-language-like types, and this comes with a few caveats that can be emphasized in tutorials and learned. If the convenience isn't worth the caveats for you, there's std.container.array and perhaps https://code.dlang.org/packages/bcaa

I believe that's good design with dynamic arrays, because their pointer and length data are passed by copy. Testing a dynamic array against null means, in practice, testing whether it's .ptr property points to null. There is no such thing as a dynamic array that is itself null (unless it has ref storage class).

Associative arrays, however, are passed around by reference. When we test those against null we test whether we have an associative array at all, not whether that associative array points to any memory area.

August 09, 2021

On 8/9/21 8:16 AM, deadalnix wrote:

>

There is an immediate problem with this: how does one gets an empty, but non null, associative array?

There isn't a "good" way to do this. The only way I know is:

a1[0] = 0;
a1.clear();

This will leave an empty, but allocated AA.

It would be pretty trivial to add this feature (make me an empty but allocated AA). Probably 2 lines of code in druntime. People have tried in the past to allow reserving memory for an AA, but it's not a trivial task (unlike this).

I think there is an issue report somewhere, let me see... No didn't find one.

-Steve

1 2
Next ›   Last »