www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - allMembers

reply Manu <turkeyman gmail.com> writes:
--20cf3074d25ca02f8f04bf35e532
Content-Type: text/plain; charset=UTF-8

So I do a lot of module scanning via allMembers, and I'm consistently
running into an awkward problem:

module test.blah;

static this()
{
  foreach(m; __traits(allMembers, test.module))
  {
    // m is a string, the only way I know to address the thing m references
is: mixin(m), and this can't be involved in virtually any complex
expressions...

    // I need to know if m is an interface, class or struct
    static if( is( mixin(m) == [interface/class/struct] ) )   // how can I
do this? I can't use mixin() here.


    // I have a scenario where I need to know if m is a function, and I
made it work using an ugly trick like this:
    static if( is( typeof( mixin( m ) ) ) ) // this works!
    {
      // here, I know that m is an instance of something, which means I can
alias it's typeof()
      alias typeof( mixin( m ) ) Type;

      // ... and life is good
      static if( is( Type == function ) )
      {
        ...
      }
    }

    // that construct doesn't work for definitions, like
interface/class/struct

  }
}

What can I do?

--20cf3074d25ca02f8f04bf35e532
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

So I do a lot of module scanning via allMembers, and I&#39;m consistently r=
unning into an awkward problem:<div><br></div><div>module test.blah;</div><=
div><br></div><div>static this()</div><div>{</div><div>=C2=A0 foreach(m; __=
traits(allMembers, test.module))</div>
<div>=C2=A0 {</div><div>=C2=A0 =C2=A0 // m is a string, the only way I know=
 to address the thing m references is: mixin(m), and this can&#39;t be invo=
lved in virtually any complex expressions...</div><div><br></div><div>=C2=
=A0 =C2=A0 // I need to know if m is an interface, class or struct</div>
<div>=C2=A0 =C2=A0 static if( is( mixin(m) =3D=3D [interface/class/struct] =
) ) =C2=A0 // how can I do this? I can&#39;t use mixin() here.</div><div><b=
r></div><div><br></div><div>=C2=A0 =C2=A0 // I have a scenario where I need=
 to know if m is a function, and I made it work using an ugly trick like th=
is:</div>
<div><div>=C2=A0 =C2=A0 static if( is( typeof( mixin( m ) ) ) ) // this wor=
ks!</div><div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 // here, I kno=
w that m is an instance of something, which means I can alias it&#39;s type=
of()</div><div>=C2=A0 =C2=A0 =C2=A0=C2=A0alias typeof( mixin( m ) ) Type;</=
div>
<div><br></div><div>=C2=A0 =C2=A0 =C2=A0 // ... and life is good</div><div>=
=C2=A0 =C2=A0 =C2=A0 static if( is( Type =3D=3D function ) )</div><div>=C2=
=A0 =C2=A0 =C2=A0 {</div></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 ...</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 }</div><div>=C2=A0 =C2=A0 }</div><div><br></div><di=
v>=C2=A0 =C2=A0 // that construct doesn&#39;t work for definitions, like in=
terface/class/struct</div>
<div><br></div><div>=C2=A0 }</div><div>}</div><div><br></div><div>What can =
I do?</div>

--20cf3074d25ca02f8f04bf35e532--
May 04 2012
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
__traits(getMember, test.module, m);

should work.
May 04 2012
prev sibling next sibling parent Manu <turkeyman gmail.com> writes:
--20cf3074d4fec02a6e04bf365b8a
Content-Type: text/plain; charset=UTF-8

On 4 May 2012 16:34, Adam D. Ruppe <destructionator gmail.com> wrote:

 __traits(getMember, test.module, m);

 should work.

Tried that: static if( is( __traits( getMember, mixin( moduleName ), m ) == interface ) ) { pragma( msg, "Is an interface: " ~ m ); } --20cf3074d4fec02a6e04bf365b8a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On 4 May 2012 16:34, Adam D. Ruppe <span dir=3D"= ltr">&lt;<a href=3D"mailto:destructionator gmail.com" target=3D"_blank">des= tructionator gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_= quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1= ex"> __traits(getMember, test.module, m);<br> <br> should work.<br> </blockquote></div><div><br></div>Tried that:<div><div><div><br></div><div>= static if( is( __traits( getMember, mixin( moduleName ), m ) =3D=3D interfa= ce ) )</div><div>{</div><div>pragma( msg, &quot;Is an interface: &quot; ~ m= );</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>}</= div></div><div><br></div></div> --20cf3074d4fec02a6e04bf365b8a--
May 04 2012
prev sibling next sibling parent Manu <turkeyman gmail.com> writes:
--20cf3005dc10ff3ba604bf3662ff
Content-Type: text/plain; charset=UTF-8

On 4 May 2012 17:01, Manu <turkeyman gmail.com> wrote:

 On 4 May 2012 16:34, Adam D. Ruppe <destructionator gmail.com> wrote:

 __traits(getMember, test.module, m);

 should work.

Tried that: static if( is( __traits( getMember, mixin( moduleName ), m ) == interface ) ) { pragma( msg, "Is an interface: " ~ m ); }

** accidental 'send' ** >_< Fails: engine\bindings.d(399):basic type expected, not __traits engine\bindings.d(399):found '__traits' when expecting ')' engine\bindings.d(399):expression expected, not 'interface' engine\bindings.d(399):found ')' instead of statement --20cf3005dc10ff3ba604bf3662ff Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On 4 May 2012 17:01, Manu <span dir=3D"ltr">&lt;= <a href=3D"mailto:turkeyman gmail.com" target=3D"_blank">turkeyman gmail.co= m</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margi= n:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div class=3D"im"><div class=3D"gmail_quote">On 4 May 2012 16:34, Adam D. R= uppe <span dir=3D"ltr">&lt;<a href=3D"mailto:destructionator gmail.com" tar= get=3D"_blank">destructionator gmail.com</a>&gt;</span> wrote:<br><blockquo= te class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc so= lid;padding-left:1ex"> __traits(getMember, test.module, m);<br> <br> should work.<br> </blockquote></div><div><br></div></div>Tried that:<div><div><div><br></div=
<div>static if( is( __traits( getMember, mixin( moduleName ), m ) =3D=3D i=

e: &quot; ~ m );</div> <div>}</div></div></div> </blockquote><div><br></div><div>** accidental &#39;send&#39; ** &gt;_&lt;= =C2=A0</div><div><br></div><div>Fails:</div><div><div>=C2=A0 engine\binding= s.d(399):basic type expected, not __traits</div><div>=C2=A0 engine\bindings= .d(399):found &#39;__traits&#39; when expecting &#39;)&#39;</div> <div>=C2=A0 engine\bindings.d(399):expression expected, not &#39;interface&= #39;</div><div>=C2=A0 engine\bindings.d(399):found &#39;)&#39; instead of s= tatement</div></div></div> --20cf3005dc10ff3ba604bf3662ff--
May 04 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, May 04, 2012 at 05:03:36PM +0300, Manu wrote:
 On 4 May 2012 17:01, Manu <turkeyman gmail.com> wrote:
 
 On 4 May 2012 16:34, Adam D. Ruppe <destructionator gmail.com> wrote:

 __traits(getMember, test.module, m);

 should work.

Tried that: static if( is( __traits( getMember, mixin( moduleName ), m ) == interface ) ) { pragma( msg, "Is an interface: " ~ m ); }

** accidental 'send' ** >_< Fails: engine\bindings.d(399):basic type expected, not __traits engine\bindings.d(399):found '__traits' when expecting ')' engine\bindings.d(399):expression expected, not 'interface' engine\bindings.d(399):found ')' instead of statement

You're missing a typeof there. __traits(getMember,...) returns the actual value of the member, not just its type. T -- Государство делает вид, что платит нам зарплату, а мы делаем вид, что работаем.
May 04 2012
prev sibling parent Manu <turkeyman gmail.com> writes:
--20cf303b4119cd4e9304bf367d03
Content-Type: text/plain; charset=UTF-8

On 4 May 2012 17:09, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:

 On Fri, May 04, 2012 at 05:03:36PM +0300, Manu wrote:
 On 4 May 2012 17:01, Manu <turkeyman gmail.com> wrote:

 On 4 May 2012 16:34, Adam D. Ruppe <destructionator gmail.com> wrote:

 __traits(getMember, test.module, m);

 should work.

Tried that: static if( is( __traits( getMember, mixin( moduleName ), m ) ==


 ) )
 {
   pragma( msg, "Is an interface: " ~ m );
 }

** accidental 'send' ** >_< Fails: engine\bindings.d(399):basic type expected, not __traits engine\bindings.d(399):found '__traits' when expecting ')' engine\bindings.d(399):expression expected, not 'interface' engine\bindings.d(399):found ')' instead of statement

You're missing a typeof there. __traits(getMember,...) returns the actual value of the member, not just its type.

Sorry, I meant to say above: I'm looking for _definitions_ (not declarations), and not variables. --20cf303b4119cd4e9304bf367d03 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On 4 May 2012 17:09, H. S. Teoh <span dir=3D"ltr= ">&lt;<a href=3D"mailto:hsteoh quickfur.ath.cx" target=3D"_blank">hsteoh qu= ickfur.ath.cx</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" st= yle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div class=3D"HOEnZb"><div class=3D"h5">On Fri, May 04, 2012 at 05:03:36PM = +0300, Manu wrote:<br> &gt; On 4 May 2012 17:01, Manu &lt;<a href=3D"mailto:turkeyman gmail.com">t= urkeyman gmail.com</a>&gt; wrote:<br> &gt;<br> &gt; &gt; On 4 May 2012 16:34, Adam D. Ruppe &lt;<a href=3D"mailto:destruct= ionator gmail.com">destructionator gmail.com</a>&gt; wrote:<br> &gt; &gt;<br> &gt; &gt;&gt; __traits(getMember, test.module, m);<br> &gt; &gt;&gt;<br> &gt; &gt;&gt; should work.<br> &gt; &gt;&gt;<br> &gt; &gt;<br> &gt; &gt; Tried that:<br> &gt; &gt;<br> &gt; &gt; static if( is( __traits( getMember, mixin( moduleName ), m ) =3D= =3D interface<br> &gt; &gt; ) )<br> &gt; &gt; {<br> &gt; &gt; =C2=A0 pragma( msg, &quot;Is an interface: &quot; ~ m );<br> &gt; &gt; }<br> &gt; &gt;<br> &gt;<br> &gt; ** accidental &#39;send&#39; ** &gt;_&lt;<br> &gt;<br> &gt; Fails:<br> &gt; =C2=A0 engine\bindings.d(399):basic type expected, not __traits<br> &gt; =C2=A0 engine\bindings.d(399):found &#39;__traits&#39; when expecting = &#39;)&#39;<br> &gt; =C2=A0 engine\bindings.d(399):expression expected, not &#39;interface&= #39;<br> &gt; =C2=A0 engine\bindings.d(399):found &#39;)&#39; instead of statement<b= r> <br> </div></div>You&#39;re missing a typeof there. __traits(getMember,...) retu= rns the<br> actual value of the member, not just its type.<br></blockquote><div><br></d= iv><div>Sorry, I meant to say above: I&#39;m looking for _definitions_ (not= declarations), and not variables.</div></div> --20cf303b4119cd4e9304bf367d03--
May 04 2012