[phobos] Preferred style for multivalue returns

Simen Kjaeraas simen.kjaras at gmail.com
Sun May 2 15:43:23 PDT 2010


Lars Tandle Kyllingstad <lars at kyllingen.net> wrote:

> What follows may be a trivial design question, but it's not the first
> time I've run into it, and I'm trying to figure out what is the "Phobos
> style" for various things.
>
> I want to add a function to the new std.process that waits for the first
> child process to terminate, and returns its exit status AND process id,
> but I'm having trouble deciding on a signature for it.
>
> What do you find to be the preferred style for functions with multiple
> return values?
>
>         // Both of these are simple, but the choice between them
>         // is kind of arbitrary (i.e. why should x be the "real"
>         //  return value?)
>        Pid waitAny(out int status);
>         int waitAny(out Pid pid);

Of these, I like #2 the best. Basically, the status is what is returned
by the child process, and thus the 'real' return value.

>        // Introducing a completely new type just for this?
>        struct WaitResult { Pid pid; int status; }
>         WaitResult waitAny();

This feels overkill to me. On the other hand, the last option does
basically the same, and I like that.

>        // I personally find the following pretty elegant, but
>         // perhaps a bit verbose?  And I don't see Tuple being
>         // used much elsewhere in Phobos. (Why?)
>        Tuple!(Pid, "pid", int, "status") waitAny();

This is my favorite. I always feel that Tuple is a bit verbose, I
believe it's due to the strings. Still, I wish it were used more,
as it is an elegant solution and one that shows off D's power
(some).

-- 
Simen


More information about the phobos mailing list