www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get module name of function

reply Alex <AJ gmail.com> writes:
How do I get the module name that a function is defined in?

I have a generic template that

auto Do(T)()
{
    pragma(msg, moduleName!T);
}

in a module


and in another module I define a function

void foo() { }

and a class

class C { }

and call Do!(typeof(foo)) and Do!(C)


but it fails for the function. I use type of because it fails 
when I do not.

Is there not some uniform way to treat types like classes and 
functions as the same for meta programming?

If I do

pragma(msg, moduleName!foo);

in the same module as foo it works.

So I guess I have to use Do(alias T)() but then that breaks the 
class(cause I use `isClass`(= is(T == class) wrapper) and it 
complains ;/

I don't understand what the difference between alias and T is.

Alias can be most things and T must be a type, sometimes they 
overlap and sometimes they don't ;/ Is there any way to convert 
one thing to another when they do overlap and to know which 
direction to go?

Do(alias T) or Do(T)?


The second can only take types, the first can take symbolic 
expressions and other stuff but not types?
Mar 29 2019
parent Alex <sascha.orlov gmail.com> writes:
On Saturday, 30 March 2019 at 05:08:48 UTC, Alex wrote:
 How do I get the module name that a function is defined in?
There is https://dlang.org/library/std/traits/module_name.html
 I have a generic template that

 auto Do(T)()
 {
    pragma(msg, moduleName!T);
 }

 in a module
This is strange, as not types are placed in a module, but symbols.
 and in another module I define a function

 void foo() { }

 and a class

 class C { }

 and call Do!(typeof(foo)) and Do!(C)


 but it fails for the function. I use type of because it fails 
 when I do not.

 Is there not some uniform way to treat types like classes and 
 functions as the same for meta programming?
Could you be more precise? Variables have types. Classes define types. Both are symbols.
 If I do

 pragma(msg, moduleName!foo);

 in the same module as foo it works.
If something works in one case, and not in another, could you create a minimal example of what does work and what does not?
 So I guess I have to use Do(alias T)() but then that breaks the 
 class(cause I use `isClass`(= is(T == class) wrapper) and it 
 complains ;/
This works: ´´´ import std.experimental.all; void main() { Do!foo; Do!C; } void foo(){} class C{} auto Do(alias s)() { pragma(msg, moduleName!s); } ´´´
 I don't understand what the difference between alias and T is.
https://tour.dlang.org/tour/en/basics/templates https://dlang.org/spec/template.html https://dlang.org/articles/templates-revisited.html http://ddili.org/ders/d.en/templates.html http://ddili.org/ders/d.en/templates_more.html
 Alias can be most things and T must be a type, sometimes they 
 overlap and sometimes they don't ;/ Is there any way to convert 
 one thing to another when they do overlap and to know which 
 direction to go?

 Do(alias T) or Do(T)?


 The second can only take types, the first can take symbolic 
 expressions and other stuff but not types?
Mar 30 2019