digitalmars.D - Name resolution in templates issue
- "JS" <js.mdnq gmail.com> Jul 13 2013
I believe this is a flaw in template design, maybe intentional or overlooked or possibly my own lack of knowledge. I have a module with templates in it. I use these templates to built up compile time constructs such as methods, properties, and fields for various things like classes and interfaces. One problem I'm having is that when I use non-built in types that are in other modules each type can't be found. The declaration/definition is correct and I have even fully qualified the names. When I manually import the module with the types then they are found. This method can't be used though since the template module shouldn't know about any module that uses it. I've tried mixin the module using moduleName on the symbols but I get a recursive error with moduleName(although I think I can get this to work at some point). The issue is that the symbols are immediately looked up in all the modules inside the template module... since they are not there, the template fails. pe Since the template only builds a string that is eventually mixed in as a string, this really shouldn't be a problem. (in fact, I know everything about the type which is how I'm able to build the declarations) e.g., I use a template in the templates module to build a string like property myType value(); the template fails because myType can be found. If myType was a global built in type it would work, except enums. I've modified the code to create property somemodule.myType value(); Which contains the declaration of myType but this too fails. If I add an import statement to the template module "import somemodule;" then the code works. But I feel adding the import statement is bad, possibly a performance hit? Although the module is almost surely loaded before it seems a bit backwards or possibly error prone since the template is used to built the module that we are trying to load. I can also probably wrap the template with a mixin template which I think would cause the template to be instantiated in the module it is used but feels wrong too(potentially slow the compiler down).
Jul 13 2013