How do "pure" member functions work?
Steven Schveighoffer
schveiguy at yahoo.com
Mon Aug 22 04:21:04 PDT 2011
On Sat, 20 Aug 2011 12:43:29 -0400, Timon Gehr <timon.gehr at gmx.ch> wrote:
> On 08/20/2011 06:24 PM, Sean Eskapp wrote:
>> == Quote from David Nadlinger (see at klickverbot.at)'s article
>>> On 8/20/11 5:13 PM, Sean Eskapp wrote:
>>>> Does marking a member function as pure mean that it will return the
>>>> same
>>>> result given the same parameters, or that it will give the same
>>>> result, given
>>>> the same parameters and given the same class/struct members?
>>> The second one, the implicit this parameter is just considered a normal
>>> argument as far as purity is concerned.
>>> David
>>
>> Wait, references and pointers are now valid for pure function arguments?
>
> There are different forms of pure functions:
>
> weakly pure:
> no mutable globals are read or written. the function may however change
> its arguments. weakly pure functions are useful mainly for the
> implementation of functions with stronger purity guarantees.
I have to just interject real quick: One aspect of pure functions that
usually is forgotten (probably because it's seldom used) is that pure
functions are not allowed to accept or return shared data.
In other words:
pure int foo(ref shared(int) i); // compiler error.
I think even without the ref, it's a compiler error, although I don't see
what the point of a shared non-reference type is...
Other than that, you got everything right.
-Steve
More information about the Digitalmars-d-learn
mailing list