Rather neat way of doing multiple return values

kris foo at bar.com
Sat Mar 17 13:00:03 PDT 2007


Tom S wrote:
> 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


Cool, but it's worth pointing out that this is probably Latin to most 
people :)



More information about the Digitalmars-d mailing list