Most basic nothrow, pure, @safe functions?

monarch_dodra monarchdodra at gmail.com
Fri Mar 21 00:03:38 PDT 2014


On Friday, 21 March 2014 at 03:04:36 UTC, Steven Schveighoffer 
wrote:
> Thinking about it some more, I see what you mean -- an unshared 
> mutex is useless.
>
> But at the same time, some "logically" pure functions cannot be 
> so without mutexes. E.g. memory allocation.

Since when does "shared" => "impure" ?

If the function takes a pointer to shared data, then you are 
explicitly saying "this function depends on this shared data". 
But as long is it isn't referencing some *other* global directly, 
it is perfectly pure.

For example:

//----
int i = 5;

void foo(int* p) pure
{++p;}

void main() pure
{
     foo(&p);
}
//----

This is "textbook" of a pure function changing global state.

----------------

So the way I see it: shared => pinter to mutex => touching it is 
fair game => can be legit pure (IMO).


More information about the Digitalmars-d mailing list