February 17, 2018 Re: Cannot make my shared PureMallocator callable in pure functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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
|
February 17, 2018 Re: Cannot make my shared PureMallocator callable in pure functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On 2/17/18 9:54 AM, ag0aep6g wrote:
> On 02/17/2018 03:04 PM, Steven Schveighoffer wrote:
>> You have to be a bit careful here. pure functions can assume nothing is happening and simply not call the function.
>
> That's only a problem when the called function is strongly pure, right?
>
> Nordlöw's methods are only weakly pure. They have mutable indirections either in the return type or in a parameter type. So calls to them should not be optimized away.
That's fine as long as you aren't going to use the allocator to make any immutable or const data ;)
-Steve
|
February 17, 2018 Re: Cannot make my shared PureMallocator callable in pure functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Saturday, 17 February 2018 at 14:54:37 UTC, ag0aep6g wrote: > Nordlöw's methods are only weakly pure. They have mutable indirections either in the return type or in a parameter type. So calls to them should not be optimized away. I found a solution at https://github.com/nordlow/phobos-next/blob/master/src/pure_mallocator.d currently successfully used at https://github.com/nordlow/phobos-next/blob/master/src/open_hashmap_or_hashset.d Works for me. Thanks! |
Copyright © 1999-2021 by the D Language Foundation