Proposal: static template(fail)

Christopher Wright dhasenan at gmail.com
Fri Dec 14 16:09:08 PST 2007


BCS wrote:
> That only works if the maintainers of the two templates work together. 
> Here is an example where they wouldn't be:
> 
> //////
> module A;
> void Func(T)(T t);  // does some stuff with a T, Expects to find a Go(T) 
> that works for T
> 
> //////
> module B;
> import A;
> void Go(T: B.Type)(T t); // library that works with B.Type and interacts 
> with A.Func
> 
> //////
> module C;
> import A;
> void Go(T: C.Thing)(T t); // different library that works with C.Thing 
> and interacts with A.Func
> 
> //////
> module D;
> import A;
> import B;
> import C;
> // use it all here
> 
> 
> Now that works as is (assuming no errors in my typing) but if you now 
> want to have a module that defines a Go(T) that uses static if's to find 
> if the T is something it can handle, you can only have one of those.

Static import for the win!

You might need some glue to hold it all together, and you'd have to 
define it at the location of use:

template Go(T) {
    static if (I can handle it) {
       struct Go {}
    } else static if (compiles, A.Go!(T)) {
       alias A.Go!(T) Go;
    } else ...
}

Okay, that's ugly, and it puts the burden closer to the user. But it 
works, and it's not a terribly common situation.



More information about the Digitalmars-d mailing list