Returning tuples from functions
Daniel Keep
daniel.keep.lists at gmail.com
Tue May 1 04:07:02 PDT 2007
niovol wrote:
> When will it be possible? I have almost no experience in programming on D. But I can't imagine how to solve my problem without using tuples.
No idea; I'd like to know myself. That said, you have a few alternatives:
1. Use out parameters. It's like returning a tuple, except you put them
inside the parentheses.
Tuple!(int,int,int) foo()
{
return Tuple!(1,2,3);
}
Becomes
void foo(out int a, out int b, out int c)
{
a = 1;
b = 2;
c = 3;
}
2. Use Tuple structs. This is a horrible, nasty trick that is very cool.
struct TupleStruct(T...)
{
T tuple;
static TupleStruct!(T) opCall(T args)
{
TupleStruct!(T) result;
foreach( i,a ; args )
result.tuple[i] = a;
return result;
}
}
TupleStruct!(int, int, int) foo()
{
return TupleStruct!(int, int, int)(1, 2, 3);
}
I believe there is just such a thing in... the standard library
somewhere. Go poking; you'll find it :P
-- Daniel
--
int getRandomNumber()
{
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
http://xkcd.com/
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d
mailing list