www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - UDA: getAttributes does not play well with topleof

reply d coder <dlang.coder gmail.com> writes:
--047d7b2e0fc92432c304d10b10be
Content-Type: text/plain; charset=ISO-8859-1

Greetings

I am trying out the new UDA functionality in the github dmd. I wanted to
find out what different fields of a class carry a particular attribute. So
I tried to look at the set of attributes on all the fields returned by
tupleof. Interestingly I got nothing. On the other hand, when I explicitly
specify a particular field in the object, the attribute is getting printed
nicely on my screen.

So my question is: Is this a bug in DMD, or am I doing something plainly
wrong?

If this is not a bug, I want to find out if there would be a way to find
what different fields of a class carry a given attribute?

Thanks and Regards
- Puneet


template Tuple(T...) {
  alias T Tuple;
}

enum Bar;
class Foo {
   Bar int a;
  int b;
}

void main() {
  Foo foo = new Foo;
  foreach(ref l; foo.tupleof) {
    alias Tuple!(__traits(getAttributes, l)) tp;
    pragma(msg, tp); // prints nothing
  }
  alias Tuple!(__traits(getAttributes, foo.a)) tp;
  pragma(msg, tp);             // prints (Bar)
}

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

<div>Greetings</div><div><br></div><div>I am trying out the new UDA functio=
nality in the github dmd. I wanted to find out what different fields of a c=
lass carry a particular attribute. So I tried to look at the set of attribu=
tes on all the fields returned by tupleof. Interestingly I got nothing. On =
the other hand, when I explicitly specify a particular=A0field=A0in the obj=
ect, the attribute is getting printed nicely on my screen.</div>

<div><br></div><div>So my question is: Is this a bug in DMD, or am I doing =
something plainly wrong?</div><div><br></div><div>If this is not a bug, I w=
ant to find out if there would be a way to find what different fields of a =
class carry a given attribute?</div>

<div><br></div><div>Thanks and Regards</div><div>- Puneet</div><div><br></d=
iv><div><br></div><div>template Tuple(T...) {</div><div>=A0 alias T Tuple;<=
/div><div>}</div><div><br></div><div>enum Bar;</div><div>class Foo {</div>

<div>=A0  Bar int a;</div><div>=A0 int b;</div><div>}</div><div><br></div><=
div>void main() {</div><div>=A0 Foo foo =3D new Foo;</div><div>=A0 foreach(=
ref l; foo.tupleof) {</div><div>=A0 =A0 alias Tuple!(__traits(getAttributes=
, l)) tp;</div>

<div>=A0 =A0 pragma(msg, tp);<span class=3D"Apple-tab-span" style=3D"white-=
space:pre">		</span>// prints nothing</div><div>=A0 }</div><div>=A0 alias T=
uple!(__traits(getAttributes, foo.a)) tp;</div><div>=A0 pragma(msg, tp); =
=A0 =A0 =A0 =A0 =A0 =A0 // prints (Bar)</div>

<div>}</div><div><br></div>

--047d7b2e0fc92432c304d10b10be--
Dec 17 2012
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-12-17 12:55, d coder wrote:

 template Tuple(T...) {
    alias T Tuple;
 }

 enum Bar;
 class Foo {
     Bar int a;
    int b;
 }

 void main() {
    Foo foo = new Foo;
    foreach(ref l; foo.tupleof) {
      alias Tuple!(__traits(getAttributes, l)) tp;
      pragma(msg, tp);// prints nothing
    }
    alias Tuple!(__traits(getAttributes, foo.a)) tp;
    pragma(msg, tp);             // prints (Bar)
 }

Try something like this: foreach (i, dummy, foo.tupleof) { alias Tuple!(__traits(getAttributes, foo.tupleof[i])) tp; pragma(msg, tp); } -- /Jacob Carlborg
Dec 17 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-12-17 15:44, d coder wrote:

 Error: first argument is not a symbol
 tuple(false)

 Regards
 - Puneet

Then I don't know. -- /Jacob Carlborg
Dec 17 2012
prev sibling next sibling parent d coder <dlang.coder gmail.com> writes:
--f46d04388ed5f3348e04d10d6c81
Content-Type: text/plain; charset=ISO-8859-1

 Try something like this:

 foreach (i, dummy; foo.tupleof)
 {
     alias Tuple!(__traits(getAttributes, foo.tupleof[i])) tp;
     pragma(msg, tp);
 }

thought that got fixed. Anyways, with your suggestion I get a strange error. Error: first argument is not a symbol tuple(false) Regards - Puneet --f46d04388ed5f3348e04d10d6c81 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"m= argin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> Try something like this:<br> <br> foreach (i, dummy; foo.tupleof)<br> {<br> =A0 =A0 alias Tuple!(__traits(getAttributes, foo.tupleof[i])) tp;<br> =A0 =A0 pragma(msg, tp);<br> }<span class=3D"HOEnZb"><font color=3D"#888888"><br> <br></font></span></blockquote><div><br></div><div>Ok so that is because re= f does not work with foreach on tuples. Somehow I thought that got fixed.</= div><div>Anyways, with your suggestion I get a strange error.</div><div> <br></div><div><div>Error: first argument is not a symbol</div><div>tuple(f= alse)</div></div><div><br></div><div>Regards</div><div>- Puneet</div><div><= br></div></div> --f46d04388ed5f3348e04d10d6c81--
Dec 17 2012
prev sibling next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 12/17/2012 3:55 AM, d coder wrote:
 So my question is: Is this a bug in DMD, or am I doing something plainly wrong?

You're doing something wrong. Consider: Bar int i = 3; int x = i; The UDA does not get transferred to x. This is why your loop: foreach(ref l; foo.tupleof) does not transfer the UDAs to l.
Dec 17 2012
parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/17/2012 8:40 AM, d coder wrote:
 But Walter, even the code below does not work.
 Error: first argument is not a symbol
 tuple(false)

That's right, msg is undefined!!
 template Tuple(T...) {
    alias T Tuple;
 }

 enum Bar;
 class Foo {
     Bar int a;
 }

 void main()
 {
    Foo foo = new Foo;
    alias Tuple!(__traits(getAttributes, foo.tupleof[0])) tp;
    pragma(msg, tp);

 }

Dec 17 2012
prev sibling next sibling parent d coder <dlang.coder gmail.com> writes:
--f46d04388ed5fc62c604d10f0bc6
Content-Type: text/plain; charset=ISO-8859-1

On Mon, Dec 17, 2012 at 9:56 PM, Walter Bright
<newshound2 digitalmars.com>wrote:

 On 12/17/2012 3:55 AM, d coder wrote:

 So my question is: Is this a bug in DMD, or am I doing something plainly
 wrong?

You're doing something wrong. Consider: Bar int i = 3; int x = i; The UDA does not get transferred to x. This is why your loop: foreach(ref l; foo.tupleof) does not transfer the UDAs to l.

Error: first argument is not a symbol tuple(false) template Tuple(T...) { alias T Tuple; } enum Bar; class Foo { Bar int a; } void main() { Foo foo = new Foo; alias Tuple!(__traits(getAttributes, foo.tupleof[0])) tp; pragma(msg, tp); } --f46d04388ed5fc62c604d10f0bc6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Mon, Dec 17, 2012 at 9:56 PM, Walter = Bright <span dir=3D"ltr">&lt;<a href=3D"mailto:newshound2 digitalmars.com" = target=3D"_blank">newshound2 digitalmars.com</a>&gt;</span> wrote:<br><bloc= kquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #cc= c solid;padding-left:1ex"> <div class=3D"im">On 12/17/2012 3:55 AM, d coder wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> So my question is: Is this a bug in DMD, or am I doing something plainly wr= ong?<br> </blockquote> <br></div> You&#39;re doing something wrong. Consider:<br> <br> Bar int i =3D 3;<br> int x =3D i;<br> <br> The UDA does not get transferred to x. This is why your loop:<br> <br> foreach(ref l; foo.tupleof)<br> <br> does not transfer the UDAs to l.<br> <br> </blockquote></div><br><div>But Walter, even the code below does not work.<= /div><div>Error: first argument is not a symbol</div><div><div>tuple(false)= </div></div><div><br></div><div><br></div><div><div>template Tuple(T...) {<= /div> <div>=A0 alias T Tuple;</div><div>}</div><div><br></div><div>enum Bar;</div=
<div>class Foo {</div><div>=A0  Bar int a;</div><div>}</div><div><br></div=
<div>void main()</div><div>{</div><div>=A0 Foo foo =3D new Foo;</div><div>=

<div>=A0 pragma(msg, tp);</div><div>}</div></div><div><br></div> --f46d04388ed5fc62c604d10f0bc6--
Dec 17 2012
prev sibling parent d coder <dlang.coder gmail.com> writes:
--047d7b2e0bff793f0d04d123fab9
Content-Type: text/plain; charset=ISO-8859-1

Greetings

I thought I would be able to use allMembers/getMembers traits to access the
attributes of all the members of a class. But that fails me if some members
of the class are private.

Consider the code pasted below -- I get en error:

B.d(5): Error: class A.Foo member bar is not accessible

And so it seems it is essential that tupleof works with getAttributes,
since tupleof does not care for access protection.

I have filed a bug on Budzilla.
http://d.puremagic.com/issues/show_bug.cgi?id=9178

Regards
- Puneet

module A;
enum Boo;
class Foo {
  private  Boo int bar;
}

module B;
void main() {
  import A;
  Foo foo = new Foo;
  static if(__traits(getProtection, __traits(getMember, foo, "bar"))  //
<-- can not access foo.bar
    == "public")
    {
      // Check for attribute Boo and proceed
    }
}

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

<div>Greetings</div><div><br></div>I thought I would be able to use allMemb=
ers/getMembers traits to access the attributes of all the members of a clas=
s. But that fails me if some members of the class are private.<div><br></di=
v>

<div>Consider the code pasted below -- I get en error:</div><div><br></div>=
<div><div>B.d(5): Error: class A.Foo member bar is not accessible</div></di=
v><div><br></div><div>And so it seems it is essential that tupleof works wi=
th getAttributes, since tupleof does not care for access protection.</div>

<div><br></div><div>I have filed a bug on Budzilla.=A0<a href=3D"http://d.p=
uremagic.com/issues/show_bug.cgi?id=3D9178">http://d.puremagic.com/issues/s=
how_bug.cgi?id=3D9178</a></div><div><br></div><div>Regards</div><div>- Pune=
et</div>

<div><br></div><div><div>module A;</div><div>enum Boo;</div><div>class Foo =
{</div><div>=A0 private  Boo int bar;</div><div>}</div><div><br></div></div=
<div><div>module B;</div><div>void main() {</div><div>=A0 import A;</div>

=A0 Foo foo =3D new Foo;</div><div>=A0 static if(__traits(getProtection, __= traits(getMember, foo, &quot;bar&quot;)) =A0// &lt;-- can not access foo.ba= r</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </spa= n> =A0 =A0=3D=3D &quot;public&quot;)</div> <div>=A0 =A0 {</div><div>=A0 =A0 =A0 // Check for attribute Boo and proceed= </div><div>=A0 =A0 }</div><div>}</div></div><div><br></div> --047d7b2e0bff793f0d04d123fab9--
Dec 18 2012