I just got it! (invariant/const)

Janice Caron caron800 at googlemail.com
Wed Apr 9 05:50:59 PDT 2008


On 09/04/2008, Jason House <jason.james.house at gmail.com> wrote:
> The strange thing is that pure functions can't call invariant member functions of their invariant data.

I don't think that's correct.

    class C
    {
        int x;

        int f() invariant pure
        {
            return x;
        }
    }

    invariant c = cast(invariant) new C;
    int n = c.f();

Should work just fine. Of course, the explicit cast necessary to
create an invariant C in the first place is a bit ugly. Maybe we need
"inew" to make new invariant objects?

Remember, a member function is nothing more nor less than a global
function with a hidden parameter and some name mangling. That means
that the rules for purity of member functions are identical to those
of global functions. You just have to insist that "this" be invariant.



More information about the Digitalmars-d mailing list