digitalmars.D - Can't declare "template parameter which takes value of any type"
- Russell Lewis (61/61) Mar 17 2008 I'd like to propose that we need a syntax for a template parameter which...
I'd like to propose that we need a syntax for a template parameter which takes "a value of any type." Of course, you can currently write templates which take values: BEGIN CODE template foo(int I) { ... whatever ... } END CODE You can also declare IFTI functions which take parameters of any type: BEGIN CODE void bar(TYPE)(TYPE argument) { ... whatever ... } END CODE But sometimes, you don't want to run a function...you want to declare a template which takes a value, but there are multiple types that the value might have. Consider this snippet from real code I'm trying to write: BEGIN CODE void DoThing(char argument, void delegate() callback) { ... whatever ... } void DoThing(string argument, void delegate() callback) { ... whatever ... } ... maybe there are lots of DoThing functions ... ... or maybe DoThing is an IFTI function ... void Chain(alias FUNC, TPL...)(void delegate() callback) { FUNC(delegate void() { static if(TPL.length > 0) Chain!(TPL)(callback); else callback(); }); } // here's where the new syntax would be *really* nice void Chain(char ARG, TPL...)(void delegate() callback) { DoThing(ARG, delegate void() { static if(TPL.length > 0) Chain!(TPL)(callback); else callback(); }); } void main() { Chain!('c','h', SomeOtherFunc, 'a','r', VariousFuncs) (delegate void() { writefln("we got a callback!"); }); } END CODE In the example above, if DoThing had a lot of overloads, then I would need to have a specialized Chain template for each type. (If Chain was part of a library, and DoThing was in some other code, this might be really difficult.) Worse, if DoThing was an IFTI function, I wouldn't be able to do this at all. However, Chain would be trivial to write if we could declare "any sort of value" parameters: BEGIN CODE THAT WON'T COMPILE void Chain(value ARG, TPL...) { ... see above ... } END CODE
Mar 17 2008