Getting module of a class

Bill Baxter wbaxter at gmail.com
Fri Oct 17 13:58:50 PDT 2008


On Fri, Oct 17, 2008 at 10:45 PM, bearophile <bearophileHUGS at lycos.com> wrote:
> Bill Baxter:
>> I'm thinking it would be nice if you could do something like
>> ModuleOf(BCDS).ANonMemberFuncton to get at one of those non-member
>> functions in BCDS's module whatever that may be.
>
> The standard and simple solution to this problem is to import just the name of the module. You can do this with a static import, and you can use an alias to shorten the module name too:
>
> static import bc = packagename.modulename;
> auto x = new bc.SomeClass(...);
> ...
> auto y = bc.nonMemberFunction(...);

The truly simplest solution here is just to ignore Scott Meyer's
advice and make nonMemberFunction a static member function.  That way
if I have access to SomeClass I will always have easy access to
nonMemberFunction, regardless of whatever round-about chain of imports
and aliases got me SomeClass.

And that's precisely what I was planning to do here.  Then it dawned
on me that Scott Meyer's advice would be a lot easier to follow in D
if there were a simple way to just get at the enclosing namespace of a
type.

Another solution would be if I could put some kind static "pointer" to
the enclosing namespace inside the type.  -- Actually that might work
well too ...  I think it should be possible to stick "alias
foo.bar.TheModule ThisModule;" inside a class.  Hmmm.

Don's solution is neat, but the fact that it's not built-in would
still mean that I'll be in the middle of writing code somewhere and
have to stop to add the import for the meta module.  And if I wasn't
using yet then I need to back up further and actually download Don's
module from somewhere etc...

--bb



More information about the Digitalmars-d mailing list