Named Tuple Names

cal callumenator at gmail.com
Tue Mar 26 10:00:40 PDT 2013


On Tuesday, 26 March 2013 at 16:31:51 UTC, Jonathan Crapuchettes 
wrote:
> but I wish there was a cleaner way to do it. If the FieldSpec 
> template
> was moved into the public section, it would allow for the name 
> member to
> be accessed.


Not sure this qualifies as clean, but it's an option:

import std.typecons, std.stdio, std.traits, std.conv;

string[] tupleNames(T)()
{
     string[] names;
     uint count = 0;
     bool matchNext = false;
	foreach(m; __traits(allMembers,T))
	{
	    if (matchNext)
	    {
	        names ~= m;
	        matchNext = false;
	        count ++;
	    }

	    if (m == "_" ~ count.to!string)
             matchNext = true;
	}
	return names;

}

pragma(msg, tupleNames!(Tuple!(int,"a",float,"b",string,"c")));

void main(){ }


More information about the Digitalmars-d-learn mailing list