Pure functions in D

Michel Fortin michel.fortin at michelf.com
Sat Sep 27 04:41:48 PDT 2008


On 2008-09-27 02:07:59 -0400, Walter Bright <newshound1 at digitalmars.com> said:

> Bruno Medeiros wrote:
>> A few questions:
>> 
>> If a pure function can throw, is it required that it always throws 
>> given the same inputs (and throwing the same Exception?).
> 
> Yes.
> 
>> Will memory allocation be considered pure?
> 
> I don't know yet. If memory allocation failure is a non-recoverable 
> exception, then pure functions can allocate memory.

Or you could go in between: allow memory allocation exceptions to be 
caught, but only when not in a pure function. This way pure functions 
still always give the same result, or fail and allow non-pure code to 
handle gracefully the situation (displaying an error message for 
instance).

Making pure functions completely non-recoverable would mean that in a 
GUI app, for instance, every time you'd call a pure function allocating 
some memory you'd run the risk of terminating the app, which is not 
really acceptable.

And I think memory allocation is a must, given that without it you 
can't create, or append to, arrays or strings, and you make pure 
functions limited to working with pre-existing buffers (which would be 
then passed by reference, preventing hoisting).

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list