Is this function pure?

Nathan Reed nathaniel.reed at gmail.com
Tue Sep 18 12:52:25 PDT 2007


Ingo Oeser wrote:
> Ok, now that's impure, since now you are using the memory by returning it.
> This is really not side effect free. You are draining a global resource,
> which is a no-no for pure functions. All the discussions and points
> raised by the other people now apply.

I think if you take this point of view, you have to conclude that even a 
function that allocates memory that becomes garbage by the end of the 
function is strictly not side-effect-free, since now some other code 
that is executing simultaneously with our "pure" function could run out 
of memory, where it wouldn't have otherwise.  In other words, memory 
allocation in a pure function would have to be totally disallowed, 
drastically reducing the usefulness of pure functions.

In fact, following the same line of reasoning, the space consumed by the 
*stack frame* of a pure function would make it strictly not 
side-effect-free.

Memory allocation shouldn't be an impediment to making a function pure. 
  If we actually run out of memory, we've got bigger problems than 
breaking the purity of the functions.

Thanks,
Nathan Reed



More information about the Digitalmars-d mailing list