C# 7 Features - Tuples

Lodovico Giaretta via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 5 08:50:31 PDT 2016


On Monday, 5 September 2016 at 15:43:43 UTC, Nick Treleaven wrote:
> Another solution is to support out argument declarations, as 
> they are a more general feature. These could then be used as 
> follows:
>
> Tuple!(int, string) fn();
> void unpack(T...)(Tuple!T, out T decls); // new phobos function
>
> fn().unpack(int i, string s);
>
> I think a combination of tuple slicing and unpack() overloads 
> could allow ignoring leading or trailing tuple fields.

We can already (almost do that):

========================================================
import std.stdio, std.typecons;

void unpack(T...)(Tuple!T tup, out T decls)
{
	static if (tup.length > 0)
	{
		decls[0] = tup[0];
		tuple(tup[1..$]).unpack(decls[1..$]);
	}
}

void main()
{
	auto t = tuple(1, "a", 3.0);
	int i;
	string s;
	double d;
	t.unpack(i, s, d);
	writeln(i);
	writeln(s);
	writeln(d);
}
========================================================


More information about the Digitalmars-d mailing list