Jump to page: 1 2
Thread overview
Cannot make my shared PureMallocator callable in pure functions
Feb 17, 2018
Nordlöw
Feb 17, 2018
Nordlöw
Feb 17, 2018
rikki cattermole
Feb 17, 2018
Nordlöw
Feb 17, 2018
rikki cattermole
Feb 17, 2018
Nordlöw
Feb 17, 2018
ag0aep6g
Feb 17, 2018
ag0aep6g
Feb 17, 2018
Nordlöw
Feb 17, 2018
Eduard Staniloiu
February 17, 2018
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?
February 17, 2018
On Saturday, 17 February 2018 at 12:33:25 UTC, Nordlöw wrote:
>     PureMallocator.instance.allocate(16);

currently errors as

pure_mallocator.d(84,16): Error: pure function 'pure_mallocator.__unittest_pure_mallocator_82_0' cannot access mutable static data 'instance'
February 17, 2018
On 17/02/2018 12:33 PM, 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 means no globals. As in none :)
February 17, 2018
On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote:
>> in pure functions?
>
> pure means no globals. As in none :)

I don't understand.

I thought std.experimental.allocators API was designed to be able express these needs, @andralex?
February 17, 2018
On 17/02/2018 12:48 PM, Nordlöw wrote:
> On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote:
>>> in pure functions?
>>
>> pure means no globals. As in none :)
> 
> I don't understand.
> 
> I thought std.experimental.allocators API was designed to be able express these needs, @andralex?

Library code cannot change the language.

pure functions cannot access globals period, it does not matter what you write. Its a hard limit. You simply don't want pure here.
February 17, 2018
On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote:
>> in pure functions?
>
> pure means no globals. As in none :)

I guess one solution is to make the member functions in PureMallocator static and change how the template argument `Allocator` for a container is used to call these member functions directly instead of via `instance`, right?
February 17, 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
February 17, 2018
On 02/17/2018 01:35 PM, rikki cattermole wrote:
> pure means no globals. As in none :)

... except immutable ones. And since PureMallocator has no fields, `instance` can be made immutable, and all the methods can be made static or const. Then they can be used in `pure` code.
February 17, 2018
On 2/17/18 8:32 AM, ag0aep6g wrote:
> On 02/17/2018 01:35 PM, rikki cattermole wrote:
>> pure means no globals. As in none :)
> 
> ... except immutable ones. And since PureMallocator has no fields, `instance` can be made immutable, and all the methods can be made static or const. Then they can be used in `pure` code.

You have to be a bit careful here. pure functions can assume nothing is happening and simply not call the function.

-Steve
February 17, 2018
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.
« First   ‹ Prev
1 2