Thread overview
Compact Printing of Tuples
Dec 14, 2014
Nordlöw
Dec 14, 2014
Tobias Pankrath
Dec 14, 2014
bearophile
December 14, 2014
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?
December 14, 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); }
December 14, 2014
Nordlöw:

> 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?

I'd like a shorter representation of tuples expecially when you have an array of them. A single tuple is acceptable to print it in the longer current form.

You can open an enhancement request.

Bye,
bearophile