www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - can't I use __traits(allMembers) recursivly ?

reply "Uplink_Coder" <someemail someprovider.some> writes:
When I try to

struct _pod_ {
   string s1;
   enum e1 { v1,v2 };
}

auto printPod(Pod)() if (__traits(isPOD,Pod)) {
	string result;
	foreach (member;__traits(allMembers,Pod) ) {
		auto _member=__traits(getMember,Pod,member);
	}
         writeln(result);
}
void main() {printPod!(_pod_);}

I get
Error: need 'this' for 's1' of type 'string'
Error: type e1 has no value
Jan 23 2014
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Thursday, 23 January 2014 at 23:42:26 UTC, Uplink_Coder wrote:
 When I try to

 struct _pod_ {
   string s1;
   enum e1 { v1,v2 };
 }

 auto printPod(Pod)() if (__traits(isPOD,Pod)) {
 	string result;
 	foreach (member;__traits(allMembers,Pod) ) {
 		auto _member=__traits(getMember,Pod,member);
 	}
         writeln(result);
 }
 void main() {printPod!(_pod_);}

 I get
 Error: need 'this' for 's1' of type 'string'
 Error: type e1 has no value
__traits(getMember, Pod, member) is the same as Pod.member. In your case Pod is _pod_, so in the end you are trying to access an non-static member via the struct type. That cannot work and the error message tells you exactly that in errorspeak. Since you are never appending to result your code has some more flaws and I'm not sure what you are trying to do? Do you try to mirror std.conv.to!string?
Jan 23 2014
parent reply "Uplink_Coder" <someemail someprovider.some> writes:
I'm trying to serialize my struct through CT-Refelction
Jan 23 2014
next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2014-01-24 07:11, Uplink_Coder wrote:
 I'm trying to serialize my struct through CT-Refelction
Here's a serialization library if you need it [1]. It will hopefully be included as std.serialization in Phobos at some point. https://github.com/jacob-carlborg/orange -- /Jacob Carlborg
Jan 23 2014
parent "Uplink_Coder" <someemail someprovider.some> writes:
Orange is neat
I had a look at but, the docs are abit lacking ...
I don't really know how to get it to do what i want

Well I have an Enum in a Struct and I need to serialze the struct 
members and "unfold" the enum.
can orange do that ?
Jan 24 2014
prev sibling parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Friday, 24 January 2014 at 06:11:30 UTC, Uplink_Coder wrote:
 I'm trying to serialize my struct through CT-Refelction
Maybe your _pod_ functions needs a parameter. // take a look at std.conv.to string toString(P)(P pod) { }
Jan 24 2014
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Friday, 24 January 2014 at 09:14:31 UTC, Tobias Pankrath wrote:
 On Friday, 24 January 2014 at 06:11:30 UTC, Uplink_Coder wrote:
 I'm trying to serialize my struct through CT-Refelction
Maybe your _pod_ functions needs a parameter. // take a look at std.conv.to string toString(P)(P pod) { }
Oops, that was not finished. __traits(getMember, pod, member) could now be used. But you'll need to check if member really is a field. So it's easier to iterate over std.traits.FieldTypeTuple directly.
Jan 24 2014
parent "Uplink_Coder" <someemail someprovider.some> writes:
I have solved by problem.
I just forgot to prepend static to the ifs I used :D
Jan 24 2014