www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - typeid of surrounding class from a static function?

reply J Arrizza <cppgent0 gmail.com> writes:
Hi,

I'm trying to get the name of the surrounding class from within a static
method of the class:

import std.stdio;

void main(string[] args)
  {
    auto o = new SomeClass();
    o.Run();
    SomeClass.StaticRun();
  }

class SomeClass
  {
  public void Run()
    {
      writeln("In Class non-static: ", typeid(typeof(this)));
    }
  public static void StaticRun()
    {
      writeln("In Class static: ", typeid(typeof(??)));
    }
  }


What do I use for "??" above.

John
Nov 06 2011
next sibling parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 06-11-2011 14:34, J Arrizza wrote:
 Hi,

 I'm trying to get the name of the surrounding class from within a static
 method of the class:

     import std.stdio;

     void main(string[] args)
        {
          auto o = new SomeClass();
          o.Run();
          SomeClass.StaticRun();
        }

     class SomeClass
        {
        public void Run()
          {
            writeln("In Class non-static: ", typeid(typeof(this)));
          }
        public static void StaticRun()
          {
            writeln("In Class static: ", typeid(typeof(??)));
          }
        }


 What do I use for "??" above.

 John
Considering the class is statically known, typeid(SomeClass)? - Alex
Nov 06 2011
parent J Arrizza <cppgent0 gmail.com> writes:
On Sun, Nov 6, 2011 at 9:14 AM, Alex R=F8nne Petersen <xtzgzorex gmail.com>=
wrote:

 Considering the class is statically known, typeid(SomeClass)?

 - Alex
Sorry, my simplified example simplified too far. I'd like to put the call into a mixin template or template, so hard-coding the class name is not an option. John
Nov 06 2011
prev sibling next sibling parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Sun, 06 Nov 2011 14:34:33 +0100, J Arrizza <cppgent0 gmail.com> wrote:

 Hi,

 I'm trying to get the name of the surrounding class from within a static
 method of the class:

 import std.stdio;

 void main(string[] args)
   {
     auto o = new SomeClass();
     o.Run();
     SomeClass.StaticRun();
   }

 class SomeClass
   {
   public void Run()
     {
       writeln("In Class non-static: ", typeid(typeof(this)));
     }
   public static void StaticRun()
     {
       writeln("In Class static: ", typeid(typeof(??)));
     }
   }


 What do I use for "??" above.
Have you tried using this? It seems to work for me: mixin template foo( ) { static void baz( ) { writeln( typeid(typeof( this ) ) ); } } class A { mixin foo!(); } void main( ) { A.baz(); // prints bar.A }
Nov 06 2011
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 11/6/11 7:34 AM, J Arrizza wrote:
 Hi,

 I'm trying to get the name of the surrounding class from within a static
 method of the class:
typeof(this) works inside static methods too. Andrei
Nov 06 2011
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 06 Nov 2011 17:55:03 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail erdani.org> wrote:

 On 11/6/11 7:34 AM, J Arrizza wrote:
 Hi,

 I'm trying to get the name of the surrounding class from within a static
 method of the class:
typeof(this) works inside static methods too.
More info: http://www.d-programming-language.org/declaration.html#typeof "typeof(this) will generate the type of what this would be in a non-static member function, even if not in a member function." -Steve
Nov 07 2011