On 01/04/2012 02:19 PM, Caligo wrote:
> I have a function that looks something like this:
>
> bool fun(double theta, out A a, out B b, out C c){  /* ... */ }
>
> if fun() returns false, then nothing is supposed to be assigned to a,
> b, c.  If it returns true, then values are assigned to a, b, c.  Also,
> there are two ways to call fun():  If I'm interested in the return
> value only, then
>
> 1. fun(theta);
>
> otherwise,
>
> 2. fun(theta, a, b, c);

How about providing the results always as return values:

import std.exception;

alias int A;
alias int B;
alias int C;

bool fun(double)
{
    return true;
}

struct ABC
{
    A a;
    B b;
    C c;
}

struct FunResult
{
    ABC * abc;

    @property bool isValid() const
    {
        return abc !is null;
    }

    @property A a() const
    {
        enforce(abc);
        return abc.a;
    }

    // etc.
}

FunResult funWithResult(double)
{
    return FunResult(new ABC);
}

void main()
{
    auto isValid = fun(1.1);

    auto result = funWithResult(2.2);
    if (result.isValid) {
        auto use = result.a;
    }
}

Ali

Reply via email to