Multiple Template Specialization Types (in D1)?

Nick Sabalausky a at a.a
Wed Jul 2 10:56:50 PDT 2008


"Jarrett Billingsley" <kb3ctd2 at yahoo.com> wrote in message 
news:g4g81l$n9b$1 at digitalmars.com...
> "Nick Sabalausky" <a at a.a> wrote in message
> news:g4g69h$j7v$1 at digitalmars.com...
>>
>> I can't decide if this is better of worse. Leaning towards worse ;)
>>
>> char[] Bar(char[] retType, char[] publicName, char[] privateName,
>>                char[] argsDecl, char[] argsCall, char[][] types)
>> {
>>    char[] ret;
>>    foreach(char[] type; types)
>>    {
>>        ret ~= "public "~retType~" 
>> "~publicName~"(T:"~type~")("~argsDecl~")
>> {"~privateName~"!(T)("~argsCall~");}";
>>    }
>>    return ret;
>> }
>>
>> mixin(Bar("void", "Foo", "Foo_dwc", "T arg", "arg", ["char", "wchar",
>> "dchar"]));
>>
>> void main(char[][] args)
>> {
>>    Foo('a');
>> }
>
> Now it's just getting silly.
>
> template MakeFunctions(RetType, char[] PublicName,
>    char[] PrivateName, Types...)
> {
>    static if(Types.length == 0)
>        const char[] MakeFunctions = "";
>    else
>        const char[] MakeFunctions =
>    "public " ~ RetType.stringof ~ " " ~ PublicName ~ "(T : " ~
>        Types[0].stringof ~ ")(T arg)\n"
>    "{\n"
>    "   return " ~ PrivateName ~ "(arg);\n"
>    "}\n"
>    ~ MakeFunctions!(RetType, PublicName, PrivateName, Types[1 .. $]);
> }
>
> public template NameOfFunc(alias f)
> {
>    // weirdness in dmdfe.
>    const char[] NameOfFunc = (&f).stringof[2 .. $];
> }
>
> private template BarImpl(char[] PublicName, alias PrivateFunc, Types...)
> {
>    alias typeof(&PrivateFunc!(Types[0])) FuncType;
>    alias ReturnTypeOf!(FuncType) RetType;
>
>    const ret = MakeFunctions!(RetType, PublicName,
>        NameOfFunc!(PrivateFunc!(Types[0])), Types);
> }
>
> public template Bar(char[] PublicName, alias PrivateFunc, Types...)
> {
>    static assert(Types.length > 0, "must have at least one type");
>    const Bar = BarImpl!(PublicName, PrivateFunc, Types).ret;
> }
>
> mixin(Bar!("Foo", Foo_dwc, char, wchar, dchar));
>
> Template-foo to the rescue, you can extract the return type directly from
> the alias to the private function.  :)
>
>

I turned that into a working compileable source file (attached, test5.d), 
and made notes of a few bits of weirdness that I noticed when getting it to 
compile.

I do actually like the end result though. Put it into a utility module, and 
it's a nicely DRY approach to the original problem that works around D1's 
lack of template constraints. (I'm a DRY junkie.)

I have a few other utility mixins I've been using. When I wrote them, I was 
having trouble getting template mixins to work without using the "CTFE that 
returns char[] and takes all-char[] paramaters" approach you saw in my 
original version of Bar. Studying that code above, I think I understand D's 
templates well enough now to maybe improve some of those other utility 
mixins I have. 


begin 666 test5.d
M;6]D=6QE('1E<W0U.PT*#0II;7!O<G0@=&%N9V\N8V]R92Y4<F%I=',[#0II
M;7!O<G0@=&%N9V\N:6\N4W1D;W5T.PT*#0IT96UP;&%T92!-86ME1G5N8W1I
M;VYS*%)E=%1Y<&4L(&-H87);72!0=6)L:6-.86UE+ T*(" @(&-H87);72!0
M<FEV871E3F%M92P at 5'EP97,N+BXI#0I[#0H@(" @<W1A=&EC(&EF*%1Y<&5S
M+FQE;F=T:" ]/2 P*0T*(" @(" @("!C;VYS="!C:&%R6UT at 36%K949U;F-T
M:6]N<R ]("(B.PT*(" @(&5L<V4-"B @(" @(" @8V]N<W0 at 8VAA<EM=($UA
M:V5&=6YC=&EO;G,@/0T*(" @(")P=6)L:6,@(B!^(%)E=%1Y<&4N<W1R:6YG
M;V8@?B B("(@?B!0=6)L:6-.86UE('X@(BA4(#H@(B!^#0H@(" @(" @(%1Y
M<&5S6S!=+G-T<FEN9V]F('X@(BDH5"!A<F<I7&XB#0H@(" @(GM<;B(-"B @
M(" B(" @<F5T=7)N("(@?B!0<FEV871E3F%M92!^("(H87)G*3M<;B(-"B @
M(" B?5QN(@T*(" @('X at 36%K949U;F-T:6]N<R$H4F5T5'EP92P at 4'5B;&EC
M3F%M92P at 4')I=F%T94YA;64L(%1Y<&5S6S$@+BX@)%TI.PT*?0T*#0IP=6)L
M:6,@=&5M<&QA=&4 at 3F%M94]F1G5N8RAA;&EA<R!F*0T*>PT*(" @("\O('=E
M:7)D;F5S<R!I;B!D;61F92X-"B @("!C;VYS="!C:&%R6UT at 3F%M94]F1G5N
M8R ]("@F9BDN<W1R:6YG;V9;,B N+B D73L-"GT-"@T*<')I=F%T92!T96UP
M;&%T92!"87));7!L*&-H87);72!0=6)L:6-.86UE+"!A;&EA<R!0<FEV871E
M1G5N8RP at 5'EP97,N+BXI#0I[#0H)+R\@4V5E;7,@=&\@=V]R:R!W:71H(&5I
M=&AE<B B4')I=F%T949U;F,B(&]R("(F4')I=F%T949U;F,B+ T*"2\O(&)U
M="!I9B!A;GD@;W1H97(@97)R;W(@97AI<W1S("(F4')I=F%T949U;F,B('=I
M;&P at 9V5N97)A=&4Z#0H)+R\@=&5S=#4N9"@S,BDZ($5R<F]R.B!T=7!L92!I
M;F1E>" P(&5X8V5E9',@, T*(" @(&%L:6%S('1Y<&5O9BA0<FEV871E1G5N
M8R$H5'EP97-;,%TI*2!&=6YC5'EP93L-"B @("!A;&EA<R!2971U<FY4>7!E
M3V8A*$9U;F-4>7!E*2!29714>7!E.PT*#0H@(" @8V]N<W0@<F5T(#T at 36%K
M949U;F-T:6]N<R$H4F5T5'EP92P at 4'5B;&EC3F%M92P-"B @(" @(" @3F%M
M94]F1G5N8R$H4')I=F%T949U;F,A*%1Y<&5S6S!=*2DL(%1Y<&5S*3L-"GT-
M"@T*<'5B;&EC('1E;7!L871E($)A<BAC:&%R6UT at 4'5B;&EC3F%M92P at 86QI
M87, at 4')I=F%T949U;F,L(%1Y<&5S+BXN*0T*>PT*(" @('-T871I8R!A<W-E
M<G0H5'EP97,N;&5N9W1H(#X@,"P@(FUU<W0@:&%V92!A="!L96%S="!O;F4@
M='EP92(I.PT*(" @(&-O;G-T($)A<B ]($)A<DEM<&PA*%!U8FQI8TYA;64L
M(%!R:79A=&5&=6YC+"!4>7!E<RDN<F5T.PT*?0T*#0HO+R!-:7AI;BA"87(A
M*"XN+BDI(&UU<W0 at 8V]M92!A9G1E<B!T:&ES(&9U;F-T:6]N(&1E8VQA<F%T
M:6]N(&]R(&5L<V4Z#0HO+R!T97-T-2YD*#,R*3H@=&5M<&QA=&4@:6YS=&%N
M8V4 at 9F]R=V%R9"!R969E<F5N8V4@=&\@=&5M<&QA=&4 at 9&5C;&%R871I;VX@
M1F]O7V1W8RA4*0T*<')I=F%T92!V;VED($9O;U]D=V,H5"DH5"!A<F<I('L@
M4W1D;W5T+F9O<FUA=&QN*")[?2(L(&%R9RD[('T-"FUI>&EN*$)A<B$H(D9O
M;R(L($9O;U]D=V,L(&-H87(L('=C:&%R+"!D8VAA<BDI.PT*#0HO+R!497-T
M(&5R<F]R(&-O;F1I=&EO;@T*+R\@5&AE('-T871I8R!A<W-E<G0 at 9&]E<VXG
M="!S965M('1O(&=E="!T<FEP<&5D(&EN($1-1" Q+C R.0T*+R\@=&5S=#4N
M9"@S,BDZ($5R<F]R.B!0<FEV871E1G5N8R$H:6YT*2!I<R!N;W0 at 86X@;'9A
M;'5E#0HO+VUI>&EN*$)A<B$H(D9O;S(B+"!&;V]?9'=C*2D[#0H-"G9O:60@
M;6%I;BAC:&%R6UU;72!A<F=S*0T*>PT*"49O;R at B8R)C6S!=*3L-"@E&;V\H
>(G<B=ULP72D[#0H)1F]O*")D(F1;,%TI.PT*?0T*
`
end



More information about the Digitalmars-d-learn mailing list