Rather neat way of doing multiple return values

nobody nobody at nix.de
Wed Apr 4 23:42:09 PDT 2007


Jesse Phillips Wrote:

> On Sat, 17 Mar 2007 13:00:03 -0700, kris wrote:
> 
> > 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 :)
> 
> I agree with you here, I tried to follow how it works but am so unfamiliar
> with the tools used I got lost and surprised by the result. :)

Very cool.
I hope, some day we can do this, like perl
($one,$two) = (split (/,/,$line))[0,2];





More information about the Digitalmars-d mailing list