Thread overview
pure member functions
Dec 26, 2008
mastrost
Dec 27, 2008
Walter Bright
Dec 27, 2008
mastrost
December 26, 2008
Hello,

I have searched on the D2 documentation pages and on this forum, but I did not really found any documentation about pure member functions, so please excuse me if a make you repeat.

I was very surprised to see this code compile (dmd 2.022):

class A{
    private:
        int x;
    public:
        pure int f() {
            return x;
        }
        int g() {
            ++x;
            return x;
        }
}

void main(){

    A a=new A;
    writefln(a.f()); //prints 0
    a.g();
    writefln(a.f()); //prints 1
}

- So what kind of function can be considered as a pure member function ?

- In my exemple, is A.f really considered as pure by the compiler, because I explicitly added the keyword 'pure' ?

- Is a D compiler a good gift for christmas?

- Is this assertion true:
    "If we wanted to have true purity for member functions, it would mean that as soon as a member function is pure, all other functions in the same module would have to be pure, expect the constructors"

- (equivalent to the previous question) If inside a module only the constructors of the classes are not pure, can I be sure that no behaviour like what we see in my example will happen (the first call to a.f() gives different result from the second call to a.f() ) ?

Thank you all

December 27, 2008
mastrost wrote:
> I was very surprised to see this code compile (dmd 2.022):

The 'this' reference must also be considered an argument to a pure function: a.f() is f(a) as far as purity checking goes.
December 27, 2008
Walter Bright Wrote:

> mastrost wrote:
> > I was very surprised to see this code compile (dmd 2.022):
> 
> The 'this' reference must also be considered an argument to a pure function: a.f() is f(a) as far as purity checking goes.

Ok thank you, I understand better now.