Thread overview
Re: Static Associative Arrays
Apr 08, 2012
Jonathan M Davis
Apr 08, 2012
H. S. Teoh
Apr 08, 2012
Caligo
Apr 08, 2012
Jonathan M Davis
Apr 12, 2012
Christophe
April 08, 2012
On Saturday, April 07, 2012 18:45:40 Caligo wrote:
> I'm not questioning the design, but I would like to know the reason: given the fact that associative arrays are built into the language, why don't we have static associative arrays?

What do you mean my static associative arrays? Are you asking why you can't initialize a static variable which is an AA at compile time? e.g.

static int[string] aa = ["hello" ; 7, "world", 13]; //error

That's because that would involve allocating memory at compile time which would then somehow have to be around at runtime. And that doesn't work right now. Classes have the same problem. You have to initialize them at runtime. I believe that dynamic arrays are the only ones that work with that right now, and that's because it's a difficult problem. Obviously, none of the memory allocated at compile time can persist to runtime, so there is no simple solution. At some point, it will probably be possible, but not right now. You _should_ be able to use them in CTFE, but they can't be assigned to anything that will persist until runtime.

The solution is to use static constructors to initialize AAs and class references at runtime.

Now, if you're talking about something else, I have no idea what you mean by static AAs.

- Jonathan M Davis
April 08, 2012
On Sat, Apr 07, 2012 at 09:01:40PM -0700, Jonathan M Davis wrote:
> On Saturday, April 07, 2012 18:45:40 Caligo wrote:
> > I'm not questioning the design, but I would like to know the reason: given the fact that associative arrays are built into the language, why don't we have static associative arrays?
> 
> What do you mean my static associative arrays? Are you asking why you can't initialize a static variable which is an AA at compile time? e.g.
> 
> static int[string] aa = ["hello" ; 7, "world", 13]; //error
> 
> That's because that would involve allocating memory at compile time which would then somehow have to be around at runtime. And that doesn't work right now. Classes have the same problem. You have to initialize them at runtime. I believe that dynamic arrays are the only ones that work with that right now, and that's because it's a difficult problem. Obviously, none of the memory allocated at compile time can persist to runtime, so there is no simple solution. At some point, it will probably be possible, but not right now. You _should_ be able to use them in CTFE, but they can't be assigned to anything that will persist until runtime.
[...]

I've thought of a way to use CTFE to generate static AA's, but I appear to be running into a CTFE bug or quirk that causes it not to work. Basically, the idea is to use CTFE to generate a series of static struct declarations (each struct being an AA slot) with precomputed references between them. Obviously, the resulting AA must be immutable, but in principle this should allow implementation of static AA's.

But I'm running into a weird issue where using a string mixin with a string constant containing the struct definitions work correctly, but if the string is generated piecewise by CTFE code then all references turn into null. I'll have to massage the code a bit into a minimal test case and open an issue on the bugtracker.


T

-- 
Windows 95 was a joke, and Windows 98 was the punchline.
April 08, 2012
On Sat, Apr 7, 2012 at 11:01 PM, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
>
> What do you mean my static associative arrays? Are you asking why you can't initialize a static variable which is an AA at compile time? e.g.
>
> - Jonathan M Davis

The same way I can create a static array:

int[4] = [1, 3, 4, 8];  // has value semantics

and dynamic arrays:

int[] = [1, 4, 2, 4];  // has reference semantics

I want an associative array that has value semantics and it's size doesn't change, just like static arrays.

P.S.
another point.  I was always under the impression that static arrays
are allocated on the stack whereas dynamic arrays are allocated on the
heap and the GC cleans them up.  After today, I'm not so sure if this
is true.  Are static arrays allocated on the stack?  if so, that would
be another reason to want to have static associative arrays.
April 08, 2012
On Sunday, April 08, 2012 01:24:02 Caligo wrote:
> On Sat, Apr 7, 2012 at 11:01 PM, Jonathan M Davis <jmdavisProg@gmx.com>
wrote:
> > What do you mean my static associative arrays? Are you asking why you
> > can't
> > initialize a static variable which is an AA at compile time? e.g.
> > 
> > - Jonathan M Davis
> 
> The same way I can create a static array:
> 
> int[4] = [1, 3, 4, 8];  // has value semantics
> 
> and dynamic arrays:
> 
> int[] = [1, 4, 2, 4];  // has reference semantics
> 
> I want an associative array that has value semantics and it's size doesn't change, just like static arrays.

Associative arrays are always on the heap. They always have reference semantics. It would be very expensive to have an AA with value semantics. They contains pointers all over the place. It would be equivalent to calling dup on them every time that you pass them to anything. And trying to put one on the stack would get very messy because all of the pointers involved. AAs are _completely_ different from dynamic and static arrays. Aside from the fact that they both have array in their name and both allow indexing of a sort, there's really no relation between them at all.

> P.S.
> another point.  I was always under the impression that static arrays
> are allocated on the stack whereas dynamic arrays are allocated on the
> heap and the GC cleans them up.  After today, I'm not so sure if this
> is true.  Are static arrays allocated on the stack?  if so, that would
> be another reason to want to have static associative arrays.

Yes. static arrays go on the stack and are value types, whereas dynamic arrays go no the heap and are reference types which are managed by the GC. If you want more details on how arrays work, read this:

http://dlang.org/d-array-article.html

- Jonathan M davis
April 12, 2012
Jonathan M Davis , dans le message (digitalmars.D.learn:34332), a
 écrit :
> On Sunday, April 08, 2012 01:24:02 Caligo wrote:
>> On Sat, Apr 7, 2012 at 11:01 PM, Jonathan M Davis <jmdavisProg@gmx.com>
> wrote:
>> > What do you mean my static associative arrays? Are you asking why you
>> > can't
>> > initialize a static variable which is an AA at compile time? e.g.
>> > 
>> > - Jonathan M Davis
>> 
>> The same way I can create a static array:
>> 
>> int[4] = [1, 3, 4, 8];  // has value semantics
>> 
>> and dynamic arrays:
>> 
>> int[] = [1, 4, 2, 4];  // has reference semantics
>> 
>> I want an associative array that has value semantics and it's size doesn't change, just like static arrays.
> 
> Associative arrays are always on the heap. They always have reference semantics. It would be very expensive to have an AA with value semantics. They contains pointers all over the place. It would be equivalent to calling dup on them every time that you pass them to anything. And trying to put one on the stack would get very messy because all of the pointers involved. AAs are _completely_ different from dynamic and static arrays. Aside from the fact that they both have array in their name and both allow indexing of a sort, there's really no relation between them at all.

Yet, one could imagine an associative array with a fixed size and fixed indexes, with no pointers. We could define a simple structure with a static array to store the data and methods to find the element associated with a key. But we could not use this structure in place of a classic associative array (for that we would need pointers obviously).