A problem with AAs

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Jan 24 12:01:45 PST 2008


"bearophile" <bearophileHUGS at lycos.com> wrote in message 
news:fnabcn$qhs$1 at digitalmars.com...
> Jarrett Billingsley:
>> That's probably the best you _can_ do.  The issue, as usual, arises from 
>> AA
>> literals being weird.
>
> Thank you for your answer, then I'll probably restrict the usage of those 
> functions, refusing at compile time the AAs with static arrays.
>
> But I think that can't be classified as a problem of AA literals, a 
> literal is a way to define something in the code. I think the problem 
> there is that the opApply() doesn't allow you to yield a reference to a 
> static array (regardless the way you have defined the AA in your source 
> code... I may want an AA with a static array as values anyway). (I don't 
> fully understand still the inner workings of opApply, do you know why it 
> has such limitation?)
>
> Bye,
> bearophile

It's not opApply, it's that static arrays are second-class types.  They are 
bizarre.

They are the only types whose .init is not the same type as themselves (try 
it!  their .init is their element type).

They cannot be returned from functions because they are allocated on the 
stack.

Although they are allocated on the stack like value types, they are never 
passed by value: passing a static array to a function actually passes the 
pointer to it, so if you modify a static array parameter in a function 
you're actually modifying the array that was passed in.

Finally, they cannot be 'ref' or 'out' (hence the issue with opApply).  I 
don't know why the compiler can't do this.

Most of this stems from them trying to be binary-compatible with C arrays. 
They are allocated on the stack, you can't return them from functions, and 
passing them passes a pointer.  I have no idea what's up with .init (D1's 
std.traits actually takes advantage of this oddity to determine if a type is 
a static array!), and the no-ref-out thing is weird.  I mean, it's because 
you can't reassign what a static array variable points to.. 




More information about the Digitalmars-d-learn mailing list