Using std traits
    JN 
    666total at wp.pl
       
    Thu Jan 25 19:49:05 UTC 2018
    
    
  
I decided it's time to learn how std traits work. I still find 
the whole compile time business a bit weird to deal with, so I 
decided to write a simple JSON serializer for struct that loops 
over member fields and outputs them.
import std.stdio;
import std.json;
import std.traits;
struct TestStruct
{
	@("noserialize") int x;
	int y;
	int z;
}
void serialize(T)(T obj)
{
	if (is(T == struct))
	{
		foreach (i, member; FieldNameTuple!T)
		{
			if (!hasUDA!(member, "noserialize"))
			{
				writeln(member);
			}
		}
	} else {
		assert(0, "Not a struct");
	}
}
void main()
{
	TestStruct ts;
	ts.x = 1;
	ts.y = 2;
	ts.z = 3;
	serialize(ts);
}
here's a runnable version: https://ideone.com/U4ROAT
I expected it to output "y" "z", but "x" is also present. What am 
I doing wrong with hasUDA?
    
    
More information about the Digitalmars-d-learn
mailing list