Compact Printing of Tuples

Tobias Pankrath via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 14 05:34:32 PST 2014


On Sunday, 14 December 2014 at 11:53:21 UTC, Nordlöw wrote:
> Why arent' Tuples defaultly printed as
>
> (1, 2)
>
> instead
>
> Tuple!(int, int)(1, 2)
>
> ?
>
> What's the most convenient of tweak this behaviour so I get 
> untyped-printing. Is there a function to convert a Tuple to 
> into a D parameter tuple?

Tuple.expand?

template Wrapper(Tuple)
{
   static if(isTuple!Tuple)
   {
     struct Wrapper(Tuple)
     {
	Tuple tup;
	alias tup this;
	// should use a better overload
	string toString()
	{
	  auto app = appender!string();
	  app.put("(");
	  app.put(to!string(wrapper(tuple[0])));
	  foreach(t; tuple.expand[1 .. $])
	  {
	    app.put(", ");
	    app.put(to!string(wrapper(t))))
	  }
	  app.put(")");
	  return app.data;
	}
     }
   }
   else
     alias Wrapper = Tuple;
}

auto wrapper(T)(T t) { return Wrapper!T(t); }


More information about the Digitalmars-d-learn mailing list