www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question Object.factory and namespaces.

reply "dcoder" <dcoder nowhere.com> writes:
Hello.

I am reading through TDPL by Andresscu.  The book has the 
following example, which I am abbreviating:


void main( string[] args) {
   Stat [] stats;

   foreach( arg; args[1..$]) {
     auto newStat = cast(Stat) Object.factory( "statsinc04." ~ 
arg);
     enforce( newStat, "Invalid statistical function: " ~ arg);
     stats ~= newStat;
   }


My module is called statsinc04.d and you can see it listed above 
in the foreach loop.  My question is, how do I change the code 
above so that it works even if I change the name of the 
file/module?  I'm looking for clean solution something like a 
macro if one actually exists.


Thanks.
Apr 12 2012
next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Thu, 12 Apr 2012 16:11:11 +0200, dcoder wrote:

 My question is, how do I change the code above so that it
 works even if I change the name of the file/module?  I'm looking for
 clean solution something like a macro if one actually exists.

Check out packageName, moduleName, and fullyQualifiedName in std.traits. Additionally, the __FILE__ token will be expanded to the name of the current file. Justin
Apr 12 2012
prev sibling next sibling parent "dcoder" <dcoder nowhere.com> writes:
On Thursday, 12 April 2012 at 15:36:25 UTC, Justin Whear wrote:
 On Thu, 12 Apr 2012 16:11:11 +0200, dcoder wrote:

 My question is, how do I change the code above so that it
 works even if I change the name of the file/module?  I'm 
 looking for
 clean solution something like a macro if one actually exists.

Check out packageName, moduleName, and fullyQualifiedName in std.traits. Additionally, the __FILE__ token will be expanded to the name of the current file. Justin

Thanks for this info. I tried it but I'm still getting some problems. Here's what I have for code: /* traits.d */ import std.stdio, std.traits; void main() { writefln( "moduleName = %s ", moduleName!(main)); return; } When I try to compile this, I get the following error: traits.d(7): Error: undefined identifier moduleName Could it be my compiler version? Here's what I have: $ dmd DMD32 D Compiler v2.055 By the way, I am doing this in cygwin, I don't know if that matters. thanks
Apr 12 2012
prev sibling parent Justin Whear <justin economicmodeling.com> writes:
 Could it be my compiler version?  Here's what I have:
 
 $ dmd
 DMD32 D Compiler v2.055

Yeah, I think those templates were added in the last release or two. The current version is 2.058 (and 2.059 is in beta).
Apr 12 2012