how to expand tuple?

Philippe Sigaud via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 2 13:39:14 PST 2014


Any time you want to return or store a group of values of different types.

In a way, tuples are just structs without a name (anonymous structs,
if you will).

Say you have function `foo' and want it to return an int and a string.
In D, you cannot do:

(int, string) foo() { ...  return (3, "abc");}

But you can do:

import std.typecons: tuple;
auto foo() { return tuple(3, "abc");}

Which is more or less equivalent to:

struct FooReturn { int i; string s;}
FooReturn foo() { return FooReturn(3, "abc");}


`Tuple' and its associated factory function `tuple' is just the
standard way to group values together in Phobos. You'll see some
functions returning Tuples, for example. Some D constructs also
recognize std.typecons.Tuple and deal with it elegantly. By defining
you own struct (like `FooReturn' in the previous example), you gain
some control on your code (you can define how people can interact with
FooReturn, you can test for it, etc), but you also lose the
possibility of easy interaction with some parts of Phobos.


More information about the Digitalmars-d-learn mailing list