Cannot make my shared PureMallocator callable in pure functions

Eduard Staniloiu edi33416 at gmail.com
Sat Feb 17 15:22:13 UTC 2018


On Saturday, 17 February 2018 at 12:33:25 UTC, Nordlöw wrote:
> I'm struggling with making
>
> https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d
>
> callable in pure functions such as here
>
> https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d#L84
>
> Shouldn't a shared
>
>     static shared PureMallocator instance;
>
> make it possible to call
>
>     PureMallocator.instance.allocate(16);
>
> in pure functions?

As the folks before me have pointed out, the language doesn't 
allow you to use globals inside pure code, excepting global 
immutables; this makes sense as once an immutable object was 
constructed it will never change.

As Steven pointed out, we are just trying to fool the compiler 
into thinking that allocators don't have side effects; in the 
case of Mallocator, we are just forwarding calls to libc's 
mallocator.

With this in mind, it looks to me that you just need to decide 
what is the best/easiest way for you to forward the calls. You 
could:
   1) make all you methods static (after all, the allocator is 
stateless)
   2) make `instance` immutable and make all the methods const



More information about the Digitalmars-d-learn mailing list