Pure functions in D

Bill Baxter wbaxter at gmail.com
Tue Sep 23 19:21:13 PDT 2008


On Wed, Sep 24, 2008 at 10:10 AM, Christopher Wright <dhasenan at gmail.com> wrote:

>> Will memory allocation be considered pure?
>
> Allocating heap memory affects global state, to whit, the garbage
> collector's state. I can't see how you can allow memory allocation.

You could potentially work out some limited form of allocation to make
life easier.
Just thinking out loud here, but what if you could specify that all
calls in a particular pure function should alloc memory from a
particular thread local pool?  That would mean you could continue
using conveniences like ~= type operations inside the function.  You'd
have to be forbidden from returning pointers into that memory, though.

> You can call new, though, in a pure function, provided it's placement new or
> scope.

And provided the constructor is pure.

--bb



More information about the Digitalmars-d mailing list