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

Reply via email to