www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting typeid/tuple of context (struct/class/function)

reply KlausO <oberhofer users.sf.net> writes:
Hi D experts,

I'm trying to make some generic logging code and thought
that more runtime type/tuple information would be helpful.
Take the following example

<code>
module testtypeid;

import std.stdio;

struct Outer
{
   static void func()
   {
     // want to log the context name
     writefln(typeid(Outer));      // this works
     // writefln(typeid(func));       // does not work

     // how to do this in a generic way ?
     // writefln(typeid(current_context));
     // writefln(typeid(current_context.parent));
   }
}

void main()
{
   Outer.func();
}
</code>

Some thoughts/questions:
1. typeid(Outer) works because structs supports typeinfo's

2. typeid(func) doesn't work because no typeinfo available for static 
functions. Maybe there is the possibility to get there with 
templates/tuples ?

3. Is there a way to get the typeinfo/tuple of the current context 
(function, struct, class, module) an expression is in ?

Maybe this gets easy when AST macros are available ?
Thanks


KlausO
Apr 18 2007
parent Don Clugston <dac nospam.com.au> writes:
KlausO wrote:
 Hi D experts,
 
 I'm trying to make some generic logging code and thought
 that more runtime type/tuple information would be helpful.
 Take the following example
 
 <code>
 module testtypeid;
 
 import std.stdio;
 
 struct Outer
 {
   static void func()
   {
     // want to log the context name
     writefln(typeid(Outer));      // this works
     // writefln(typeid(func));       // does not work
 
     // how to do this in a generic way ?
     // writefln(typeid(current_context));
     // writefln(typeid(current_context.parent));
   }
 }
 
 void main()
 {
   Outer.func();
 }
 </code>
 
 Some thoughts/questions:
 1. typeid(Outer) works because structs supports typeinfo's
 
 2. typeid(func) doesn't work because no typeinfo available for static 
 functions. Maybe there is the possibility to get there with 
 templates/tuples ?
typeof(func).stringof
 3. Is there a way to get the typeinfo/tuple of the current context 
 (function, struct, class, module) an expression is in ?
Technically, yes (I hacked a method to do it, by deciphering the .mangleof property). But in practice, no.
 Maybe this gets easy when AST macros are available ?
It doesn't have much to do with macros, though it is important for metaprogramming. An obvious way to do it would be to allow properties on 'scope'. scope.stringof; scope.tupleof; scope.membersof; // all variables in local scope. Or something like that.
Apr 26 2007