Rather neat way of doing multiple return values

Tom S h3r3tic at remove.mat.uni.torun.pl
Sat Mar 17 12:28:36 PDT 2007


Downs wrote:
> Here's a (imnsho) rather neat implementation of multiple return values 
> from functions.
> 
> (...)
> Have fun! :D

Hehehe nice :) Here's my version:


---

import std.stdio;
import std.bind;



struct PtrTuple(P, V) {
	P ptrs;

	void opAssign(V v) {
		foreach (i, x; v.value) {
			*ptrs.value[i] = x;
		}
	}
}



PtrTuple!(PointerTuple!(Tuple!(T)), Tuple!(T)) tup(T ...)(inout T t) {
	PtrTuple!(PointerTuple!(Tuple!(T)), Tuple!(T)) ptrs;
				
	foreach (i, dummy_; t) {
		ptrs.ptrs.value[i] = &t[i];
	}

	return ptrs;
}


Tuple!(int, float, char) someFunc(int i) {
	return tuple(i, 0.1f * i, cast(char)i);
}



void main() {
	int a;
	float b;
	char c;

	tup(a, b, c) = someFunc(55);

	writefln("a = %s", a);
	writefln("b = %s", b);
	writefln("c = %s", c);
}

---

Maybe Phobos could use such a utility, as it (apparently) gets 
reimplemented numerous times ?


--
Tomasz Stachowiak



More information about the Digitalmars-d mailing list