Thread overview
[phobos] Preferred style for multivalue returns
May 02, 2010
Simen Kjaeraas
May 02, 2010
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);


        // Introducing a completely new type just for this?

        struct WaitResult { Pid pid; int status; }
        WaitResult waitAny();


        // 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();

        ...others?


-Lars

May 03, 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
May 03, 2010


Simen Kjaeraas wrote:
> Lars Tandle Kyllingstad <lars-9JrgYCcAG2kcWVvVuXF20w at public.gmane.org> wrote:
>>        // 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).

That would be my fave as well. Its use is picking up, and I think it should become the preferred Phobos style. It's self-descriptive and very convenient to use with auto.

Andrei