Re: Inconsistent behavior of __FILE__ within mixin template
On Wednesday, 29 May 2019 at 16:08:11 UTC, Exil wrote: On Wednesday, 29 May 2019 at 08:45:45 UTC, Andre Pany wrote: [...] I imagine __FILE__ is used where the code is defined, since it is defined in "a.d" that is what is used. If you want to know the file name of where it is used then you can add it as part of the template. mixin template UnitTest(string filename = __FILE__) { private static this() { testClasses ~= TestClass(this.classinfo.name, filename ); } } Thanks a lot. That looks great. Kind regards Andre
Re: Inconsistent behavior of __FILE__ within mixin template
On Wednesday, 29 May 2019 at 08:45:45 UTC, Andre Pany wrote: Hi, I have a module a.d --- struct TestClass { string name; string fileName; } TestClass[] testClasses; mixin template UnitTest() { private static string getFileName(string fileName = __FILE__) { return fileName; } private static this() { testClasses ~= TestClass(this.classinfo.name, getFileName()); } } and a module b.d --- import std.stdio; import a; class MyTest { mixin UnitTest; this() { writeln(getFileName()); } } void main() { new MyTest(); writeln(testClasses); } What I want is to have in the struct array testClasses the file name of module b to generate an xml report. But the output of this application is b.d [TestClass("b.MyTest", "a.d")] I would have thought __FILE evaluates in both cases to "b.d" as the code is mixed into module b. Is this the intended behavior? Kind regards André I imagine __FILE__ is used where the code is defined, since it is defined in "a.d" that is what is used. If you want to know the file name of where it is used then you can add it as part of the template. mixin template UnitTest(string filename = __FILE__) { private static this() { testClasses ~= TestClass(this.classinfo.name, filename ); } }
Inconsistent behavior of __FILE__ within mixin template
Hi, I have a module a.d --- struct TestClass { string name; string fileName; } TestClass[] testClasses; mixin template UnitTest() { private static string getFileName(string fileName = __FILE__) { return fileName; } private static this() { testClasses ~= TestClass(this.classinfo.name, getFileName()); } } and a module b.d --- import std.stdio; import a; class MyTest { mixin UnitTest; this() { writeln(getFileName()); } } void main() { new MyTest(); writeln(testClasses); } What I want is to have in the struct array testClasses the file name of module b to generate an xml report. But the output of this application is b.d [TestClass("b.MyTest", "a.d")] I would have thought __FILE evaluates in both cases to "b.d" as the code is mixed into module b. Is this the intended behavior? Kind regards André
Re: Behavior of __FILE__ in mixin template
On Wednesday, 22 June 2016 at 17:52:26 UTC, Ali Çehreli wrote: On 06/22/2016 10:07 AM, Andre Pany wrote: > Hi, > > I thought a mixin template is copied into the place where the mixin > statement > exists and then the coding is evaluated. > This seems not to be true for __FILE__ Apparently its the whole template that supports that. Is moving the 'file' parameter to the entire template acceptable? mixin template formTemplate(string file = __FILE__) { // ... } Ali Perfekt, thanks a lot. Kind regards André
Re: Behavior of __FILE__ in mixin template
On 06/22/2016 10:07 AM, Andre Pany wrote: > Hi, > > I thought a mixin template is copied into the place where the mixin > statement > exists and then the coding is evaluated. > This seems not to be true for __FILE__ Apparently its the whole template that supports that. Is moving the 'file' parameter to the entire template acceptable? mixin template formTemplate(string file = __FILE__) { // ... } Ali
Behavior of __FILE__ in mixin template
Hi, I thought a mixin template is copied into the place where the mixin statement exists and then the coding is evaluated. This seems not to be true for __FILE__ I have a module form, which has a class Form. This module also contains following mixin template mixin template formTemplate() { void generateForm() { string getFormName(string file = __FILE__)() { import std.string: indexOf; return file[0..file.indexOf(".")]~".frm"; } enum fileContent = import(getFormName()); } } In another module myform I have a class MyForm in which I call the mixin statement: mixin formTemplate. The purpose of the template coding is: The enum fileContent should contain the text of a file, which has the same name as the module but the file ending .frm. I tried different things, but either it doesn't compile or __FILE__ returns "form" instead of "myform". Is this behavior correct? Do you have any tip? Kind regards André