www.digitalmars.com         C & C++   DMDScript  

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

reply J Arrizza <cppgent0 gmail.com> writes:
--90e6ba6e89d6ab86ea04b110ff80
Content-Type: text/plain; charset=ISO-8859-1

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

--90e6ba6e89d6ab86ea04b110ff80
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi,<div><br></div><div>I&#39;m trying to get the name of the surrounding cl=
ass from within a static method of the class:</div><div><br>
</div><blockquote class=3D"webkit-indent-blockquote" style=3D"margin: 0 0 0=
 40px; border: none; padding: 0px;"><div><div><div><font class=3D"Apple-sty=
le-span" face=3D"&#39;courier new&#39;, monospace"><div>import std.stdio;</=
div>
<div><br></div><div>void main(string[] args)</div><div>=A0 {</div><div>=A0 =
=A0 auto o =3D new SomeClass();</div><div>=A0 =A0 o.Run();</div><div>=A0 =
=A0 SomeClass.StaticRun();</div><div>=A0 }</div><div><br></div><div>class S=
omeClass</div><div>
=A0 {</div><div>=A0 public void Run()</div><div>=A0 =A0 {</div><div>=A0 =A0=
 =A0 writeln(&quot;In Class non-static: &quot;, typeid(typeof(this)));</div=
<div>=A0 =A0 }</div><div>=A0 public static void StaticRun()</div><div>=A0 =

ypeof(??)));</div> <div>=A0 =A0 }</div><div>=A0 }</div><div><br></div></font></div></div></div=
</blockquote><div><div><br></div></div><div>What do I use for &quot;??&quo=

--90e6ba6e89d6ab86ea04b110ff80--
Nov 06 2011
next sibling parent =?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
prev sibling next sibling parent J Arrizza <cppgent0 gmail.com> writes:
--90e6ba1eee6e98550804b116e493
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

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 --90e6ba1eee6e98550804b116e493 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Sun, Nov 6, 2011 at 9:14 AM, Alex R=F8nne Pet= ersen <span dir=3D"ltr">&lt;<a href=3D"mailto:xtzgzorex gmail.com">xtzgzore= x gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" styl= e=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div><div></div><div class=3D"h5"><br></div></div> Considering the class is statically known, typeid(SomeClass)?<br><font colo= r=3D"#888888"> <br> - Alex<br> </font></blockquote></div><br>Sorry, my simplified example simplified too f= ar. I&#39;d like to put the call into a mixin template or template, so hard= -coding the class name is not an option.<div><br></div><div>John<br><br> </div> --90e6ba1eee6e98550804b116e493--
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 next sibling parent 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
prev sibling 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