Cannot make my shared PureMallocator callable in pure functions
Steven Schveighoffer
schveiguy at yahoo.com
Sat Feb 17 13:31:41 UTC 2018
On 2/17/18 7:33 AM, 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?
Pure functions cannot access shared or thread-local data. They are not
supposed to have any side effects.
Keep in mind, allocators have side effects, we just pretend they don't.
You need to fool the compiler into thinking you aren't doing anything to
global data.
The design of allocators makes this difficult. I suggested at one point
in the past that such allocators make all functions static, which solves
other problems. Oh, I even made a PR:
https://github.com/dlang/phobos/pull/4288
But it wasn't to allow purity, it was to allow storage of an "instance"
anywhere.
-Steve
More information about the Digitalmars-d-learn
mailing list