Sorry, I just love templates, AAs and mixins :)
Saaa
empty at needmail.com
Sun Oct 18 19:33:16 PDT 2009
Ellery Newcomer wrote:
> grauzone 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;`);
>>> }
>>> else
>>> {
>>> (*elements).length = (*elements).length + 1;
>>> (*elements)[(*elements).length-1] = element;
>>> }
>>> }
>>>
>>
>> It's unreadable.
>
> No it isn't. It's an obfusticated version of
>
> if( key in var) var[key] ~= element;
> else var[key] = [element];
No, it isn't. It's obfusticated version of the generic version of that code
:)
I always forget ~= and somehow [] with a single variable in it looks
strange, my bad!
So, thanks!
This is why I posted it in the first place; could you maybe look over DData
as well :)
I somehow like that I can tell the compiler precisely(mixins) how to
generate generic(templates) code..
>
> but who wants to write that boring code? :)
Isn't your code slower because it always needs to search for the key twice
:P
Here is the new and improved version :
public void addToAAA(char[] var_name, KT, ET)(KT key, ET element)
{
mixin(ET.stringof~`[]* elements = key in `~var_name~`;`);
if( elements == null )
{
mixin(var_name~`[key] = [element];`);
}
else
{
(*elements) ~= element;
}
}
More information about the Digitalmars-d-learn
mailing list