Sorry, I just love templates, AAs and mixins :)

Saaa empty at needmail.com
Sun Oct 18 19:35:13 PDT 2009


Ary Borenszweig wrote:
> Saaa wrote:
>> public void addToAA(char[] var_name, KT, ET)(KT key, ET element)
>> {
>>   mixin(ET.stringof~`[]* elements = key in `~var_name~`;`);
>>   if( elements == null )
>>   {
>>     ET[] temp;
>>     temp.length = 1;
>>     temp[0] = element;
>>     mixin(var_name~`[key] = temp;`);
>
> Why `key`? Where's `key` defined?
Here: ...(KT key, ET element)
>
>>   }
>>   else
>>   {
>>     (*elements).length = (*elements).length + 1;
>>     (*elements)[(*elements).length-1] = element;
>
> I don't understand this. Key is not used.
That's because there is already a link to the key through elements.

>
>>   }
>> }
>
> And how do you use it? I tried to but I failed.
You need a AA defined like this:
BaseType[][KeyType] AAname;

addToAA!("AAname")(KeyType key, BaseType value);

> Also passing a string as var_name is not nice. Isn't it better to write 
> something like:
>
> char[int] x;
> x.add(1, 'h');
>
> ?

The string is the actual variable name so I think that way doesn't work, 
right?

Check the new version(shorter)  in my other reply :D




More information about the Digitalmars-d-learn mailing list