pure or not pure?

Janice Caron caron800 at googlemail.com
Thu Apr 10 10:41:15 PDT 2008


On 10/04/2008, Koroskin Denis <2korden+dmd at gmail.com> wrote:
>  OK, new is 'special', but what about malloc or my own allocator? (I happen
> to work in a gamedev and we never use new nor malloc).
>  I don't see how
>
>  special extern(C) void* malloc(size_t size);
>
>  differs from:
>
>  void* my_malloc(size_t size)
>  {
>    return malloc(size);
>  }
>
>  and why can't it be 'special' too.

Because you're modifying global state.

One of the reasons why new is special is because you never have to
delete (because the garbage collector takes care of it). This is
directly comparable to, for example, Haskell, where new "things" are
created all the time, and are never freed. It's built into the system.
More - it /is/ the system. That's the status of new. It's the de facto
heap allocation method in D.

If you want to use custom allocators, there's nothing to stop you, but
that doesn't change the fact that the minute you modify global state,
you have a function that isn't pure.

I think you're worrying too much. There should never be a need to call
malloc and free inside a pure function. If you need a function that
does impure stuff, just write the function anyway, and don't call it
pure.



More information about the Digitalmars-d mailing list