digitalmars.D - Accessing UDA of private field
- Jacob Carlborg (32/32) Jan 06 2013 I'm trying to adapt my serialization library, Orange, to use the new
- d coder (6/6) Jan 06 2013 Hello Jacob
- Philippe Sigaud (11/16) Jan 06 2013 You can use a string mixin:
- Jacob Carlborg (4/15) Jan 06 2013 Good thinking. It's not pretty but it works. Thanks.
- Philippe Sigaud (1/2) Jan 06 2013 Maybe it can be hidden inside a template?
- Jacob Carlborg (4/6) Jan 07 2013 Yeah, I'll see what I can do.
- Tove (12/19) Jan 07 2013 in which context does private fail? I'm using something like this:
- Jacob Carlborg (4/15) Jan 07 2013 Using a mixin works.
- Tove (16/32) Jan 07 2013 but this seems to work too?
- David Nadlinger (4/19) Jan 07 2013 »private« means »accessible from this module« – are you running
- d coder (4/15) Jan 06 2013 Hmm....
- Philippe Sigaud (6/24) Jan 06 2013 Yes, but the OP question was to get the attributes in a generic way. The
I'm trying to adapt my serialization library, Orange, to use the new UDA's. Currently I have one template, NonSerialized, that I want to transfer to an UDA. This template works like: class Foo { int a; int b; mixin NonSerialized!(b); } When serializing Foo "b" will not be serialized. What the template does is adding a static field to the class, like this: class Foo { int a; int b; static enum __nonSerialized = ["b"]; } Which I then can extract and use to exclude the fields from serialization. This works fine to transfer to an UDA except in one case. That is when the field is not public. Orange will serialize all fields, regardless of the protection they have. I accomplish this by using the "tupleof" property of the class. The problem is that to access a UDA attached to a field I need to pass a symbol to the __traits(getAttributes). With "tupleof" I can get the name, type and value of a field but I cannot get a symbol. __traits(getMember) can be used to get the symbol but that will only work for public fields. Does anyone have another solution? I had really hoped I could use UDA's in Orange. https://github.com/jacob-carlborg/orange -- /Jacob Carlborg
Jan 06 2013
Hello Jacob I believe this would be possible using topleof once this http://d.puremagic.com/issues/show_bug.cgi?id=9178 issue is taken care of. Kindly vote it up. :-) Regards - Puneet
Jan 06 2013
The problem is that to access a UDA attached to a field I need to pass a symbol to the __traits(getAttributes). With "tupleof" I can get the name, type and value of a field but I cannot get a symbol. __traits(getMember) can be used to get the symbol but that will only work for public fields.You can use a string mixin: class Foo { int a; (3) private int b; } void main() { writeln(mixin("__traits(getAttributes, " ~ Foo.tupleof[1].stringof ~ ")")); // -> 3 }
Jan 06 2013
On 2013-01-06 18:29, Philippe Sigaud wrote:You can use a string mixin: class Foo { int a; (3) private int b; } void main() { writeln(mixin("__traits(getAttributes, " ~ Foo.tupleof[1].stringof ~ ")")); // -> 3 }Good thinking. It's not pretty but it works. Thanks. -- /Jacob Carlborg
Jan 06 2013
Good thinking. It's not pretty but it works. Thanks.Maybe it can be hidden inside a template?
Jan 06 2013
On 2013-01-06 23:33, Philippe Sigaud wrote:Good thinking. It's not pretty but it works. Thanks. Maybe it can be hidden inside a template?Yeah, I'll see what I can do. -- /Jacob Carlborg
Jan 07 2013
On Monday, 7 January 2013 at 10:19:45 UTC, Jacob Carlborg wrote:On 2013-01-06 23:33, Philippe Sigaud wrote:in which context does private fail? I'm using something like this: struct my_struct { private: (1) int t1; (2) int t2; (3) int t3; } foreach(m; __traits(allMembers, my_struct)) with(my_struct.init) pragma(msg, __traits(getAttributes, mixin(m)));Good thinking. It's not pretty but it works. Thanks. Maybe it can be hidden inside a template?Yeah, I'll see what I can do.
Jan 07 2013
On 2013-01-07 12:59, Tove wrote:in which context does private fail? I'm using something like this: struct my_struct { private: (1) int t1; (2) int t2; (3) int t3; } foreach(m; __traits(allMembers, my_struct)) with(my_struct.init) pragma(msg, __traits(getAttributes, mixin(m)));Using a mixin works. -- /Jacob Carlborg
Jan 07 2013
On Monday, 7 January 2013 at 13:36:47 UTC, Jacob Carlborg wrote:On 2013-01-07 12:59, Tove wrote:but this seems to work too? import std.traits; struct my_struct { private: (1) int t1; (2) int t2; (3) int t3; } void main() { foreach(m; __traits(allMembers, my_struct)) pragma(msg, __traits(getAttributes, __traits(getMember, my_struct, m))); }in which context does private fail? I'm using something like this: struct my_struct { private: (1) int t1; (2) int t2; (3) int t3; } foreach(m; __traits(allMembers, my_struct)) with(my_struct.init) pragma(msg, __traits(getAttributes, mixin(m)));Using a mixin works.
Jan 07 2013
On Monday, 7 January 2013 at 15:37:48 UTC, Tove wrote:but this seems to work too? import std.traits; struct my_struct { private: (1) int t1; (2) int t2; (3) int t3; } void main() { foreach(m; __traits(allMembers, my_struct)) pragma(msg, __traits(getAttributes, __traits(getMember, my_struct, m))); }»private« means »accessible from this module« – are you running your tests with my_struct defined in a different module? David
Jan 07 2013
You can use a string mixin: class Foo { int a; (3) private int b; } void main() { writeln(mixin("__traits(getAttributes, " ~ Foo.tupleof[1].stringof ~ ")")); // -> 3 }Hmm.... This works only when main is in the same file (and therefor module) as Foo. Regards - Puneet
Jan 06 2013
On Sun, Jan 6, 2013 at 7:46 PM, d coder <dlang.coder gmail.com> wrote:You can use a string mixin:Yes, but the OP question was to get the attributes in a generic way. The interesting part is the mixin, the rest is just scaffolding to print a result. If Jacob adopts this solution, he can insert these mixins where he needs them.class Foo { int a; (3) private int b; } void main() { writeln(mixin("__traits(getAttributes, " ~ Foo.tupleof[1].stringof ~ ")")); // -> 3 }Hmm.... This works only when main is in the same file (and therefor module) as Foo.
Jan 06 2013