Proposal: static template(fail)

BCS ao at pathlink.com
Fri Dec 14 16:14:11 PST 2007


Reply to Christopher,

> 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.
> 

Garsh Darn It!!! while that doesn't even get close to doing what I wanted 
(I think) it does bring up the point that what I was proposing doesn't even 
work because the template function in A doesn't import anything with regards 
to B and C. The only way to make it work would be to mixin stuff from A into 
D and that just gets a bit nasty.  <grumble/>

OTOH it brings up an interesting question, what syntactic scope does a template 
get instanced in. The scope it's defined in, the scope it's used in or the 
scope the thing it's specialized on is defined in. Or is it come combination 
of the three?


module A;
struct B {}
struct C { B b; }
void Do(T)(T t){writef("hello world\n");}

module B
void Do(T)(T t){Do(t.b);}

module C;
import A;
import B;
C c;
Do(c);


what happens? B.Do can see into A b/c it can see A.C.b so can it see A.Do 
as well?





More information about the Digitalmars-d mailing list