digitalmars.D.learn - Inconsistent behavior of __FILE__ within mixin template
- Andre Pany (46/48) May 29 2019 Hi,
- Exil (12/61) May 29 2019 I imagine __FILE__ is used where the code is defined, since it is
- Andre Pany (4/18) May 29 2019 Thanks a lot. That looks great.
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 isb.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é
May 29 2019
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 isI 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 ); } }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é
May 29 2019
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:Thanks a lot. That looks great. Kind regards Andre[...]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 ); } }
May 29 2019