Steven Schveighoffer:

> Freeing and allocating memory is fair game for pure functions.

Allocating arrays and objects is possible in pure D functions, despite the 
memory pointers they contain (like the ptr of a returned array) are different 
across different calls. This makes those function only formally pure and 
requires care from both the programmer and the optimizations done by the 
compiler, to avoid some bad bugs :-)

Example: the C calloc() function is not considered pure, despite what it does 
is not so far from a pure D function that allocates and returns a dynamic array 
of ubytes:

import core.stdc.stdlib: calloc;

// OK
pure ubyte* foo1(int n) {
    auto a = new ubyte[n];
    return a.ptr;
}

// Error: pure function 'foo2' cannot call impure function 'calloc'
pure ubyte* foo2(int n) {
    return cast(ubyte*)calloc(n, 1);
}

void main() {}


Regarding freeing memory in pure functions, I am even less sure.
 

> On closing a file, you couldn't close that file unless the function to  
> close it was marked pure.  I would *hope* that the C call to close a file  
> was not marked as pure.

Changing the state of a file is definitively a not pure operation, even for the 
quite relaxed standards of purity of D :-)

Bye,
bearophile

Reply via email to