Do pure functions solve the "return const" problems?

Janice Caron caron800 at googlemail.com
Sat Apr 26 23:29:19 PDT 2008


On 27/04/2008, Christopher Wright <dhasenan at gmail.com> wrote:
>  An out parameter is a return value. What else is there to get?

An out parameter is global state. Sometimes literally:

    void foo(out int x, int y) { x = y; }

    foo(globalVariable, 3);

Also, as has been repeatedly pointed out, one of the benefits of pure
is that given an expression like

    f(x,y) + g(x,y)

the compiler is free to evaluate f first then g, or g first then f, at
its discression. It is also free not to call either of these functions
at all if it has a previously cached result handy. Out parameters must
be forbidden, or those assumptions fail.

If you want to return multiple values, use a tuple.

    import std.typecons;

    Tuple!(int,double) f(int x, double y) pure
    {
        return tuple(x+1, y+1);
    }

It's not rocket science.



More information about the Digitalmars-d mailing list