How to specialize template function for associative arrays?

Aarti_pl aarti at interia.pl
Fri Oct 19 07:24:41 PDT 2007


Regan Heath pisze:
> Aarti_pl wrote:
>> Extrawurst pisze:
>>> as far as i know:
>>>
>>> void parse(T : A[B])() {
>>> }
>>>
>>> ~Extrawurst
>>>
>>>
>>
>> Unfortunately not... I was also thinking that it will work, but you 
>> get compile time error instead:
>>
>> quicktest.d(13): Error: identifier 'B' is not defined
>> quicktest.d(13): Error: index is not a type or an expression
>> quicktest.d(13): Error: identifier 'A' is not defined
>> quicktest.d(13): Error: A is used as a type
>>
>> I managed to catch associative arrays with 'static if' and templates, 
>> but it would be better to use template specialization to have better 
>> source code structure.
>>
>> Any other ideas?
> 
> Does parse really have no arguments?  Or does it actally look a bit like:
> 
> import std.stdio;
> 
> void parse(T)(T t) {
>     writefln("parse(T)");
> }
> 
> 
> void parse(V, K)(V[K] arr) {
>     writefln("parse(V, K)");
> }
> 
> void main() {
>     int[char[]] arr;
>     parse!(int, char[])(arr);
> }
> 
> Regan

Nope. I just want to choose proper template function depending on 
parameter type (in this case associative array).

It should be something similar to:
void parse(T : T[])() {
     writefln("parse(T[])");
}
which actually works.

Maybe it is just doesn't work in D?

-----------

BTW: above syntax for normal arrays looks for me not very consistent. In 
fact it should be:
# void parse(T : E[])() {
#     writefln("Type: ", T.stringof, " Element type: ", A.stringof);
# }

and then proposal from Extrawurst would be just logical consequence.

BR
Marcin


More information about the Digitalmars-d-learn mailing list