Module name compile time constant?
Don
nospam at nospam.com.au
Sat Apr 26 12:32:14 PDT 2008
Bruno Medeiros wrote:
> davidl wrote:
>> 在 Tue, 15 Apr 2008 13:00:57 +0800,Bill Baxter
>> <dnewsgroup at billbaxter.com> 写道:
>>
>>> Walter Bright wrote:
>>>> davidl wrote:
>>>>>
>>>>> module abc.super.long.pack.awesome.mod;
>>>>>
>>>>> what if i need the string of "abc.super.long.pack.awesome.mod"
>>>>> generally and don't want to copy paste differently in all my modules?
>>>> alias abc.super.long.pack.awesome.mod foo;
>>>
>>> I think he wants that full module name as a char[], though.
>>>
>>> Is that what you want, Davidl?
>>>
>>> Or do you just want to be able to say
>>>
>>>
>>> import short.awesome; // aka "import
>>> abc.super.long.pack.awesome.mod"
>>>
>>> If that's what you want you can create a module in short/awesome.d
>>> that contains just:
>>>
>>> module short.awesome;
>>> public import abc.super.long.pack.awesome.mod;
>>>
>>> But somehow I'm thinking you actually want to automatically get that
>>> module name as a string.
>>>
>>> How about going the other way around? Start with a string?
>>>
>>> string foo = "abc.super.long.pack.awesome.mod";
>>> mixin(import_module(foo));
>>>
>>> where import_module is
>>>
>>> string import_module(string name) {
>>> return "import " ~ name ";" ;
>>> }
>>>
>>> If those don't answer your question you're gonna have to be more
>>> specific.
>>>
>>>
>>> --bb
>>
>> Yes, i want the string. But I don't want to modify the module part to
>> manually defined strings
>> I want some magic of module.name to get the current module name in
>> compile time string form.
>>
>
> I tried this to see if it would work:
>
> --- ---
> module foo.test;
>
> void main()
> {
> writefln(.stringof);
> }
>
>
> Almost worked! It prints "module test". It would do what you wanted if
> '.stringof' returned the fully qualified module (and didn't have the
> "module " prefix).
Maybe it's supposed to. The spec for .stringof is completely different
to what's implemented.
But anyway...
Getting the fully qualified name is currently possible (since about
D0.150) but it's a bit disgusting to do, and I can't really recommend
it. My old meta.nameof module did the job.
If you pass the module name as a template alias parameter, the fully
qualified name appears in the mangled name. You just need to demangle it
and extract the module name.
More information about the Digitalmars-d
mailing list