digitalmars.D.learn - Cannot make my shared PureMallocator callable in pure functions
- =?UTF-8?B?Tm9yZGzDtnc=?= (9/9) Feb 17 2018 I'm struggling with making
- =?UTF-8?B?Tm9yZGzDtnc=?= (5/6) Feb 17 2018 currently errors as
- rikki cattermole (2/20) Feb 17 2018 pure means no globals. As in none :)
- =?UTF-8?B?Tm9yZGzDtnc=?= (5/7) Feb 17 2018 I don't understand.
- rikki cattermole (4/13) Feb 17 2018 Library code cannot change the language.
- =?UTF-8?B?Tm9yZGzDtnc=?= (6/8) Feb 17 2018 I guess one solution is to make the member functions in
- ag0aep6g (4/5) Feb 17 2018 ... except immutable ones. And since PureMallocator has no fields,
- Steven Schveighoffer (4/10) Feb 17 2018 You have to be a bit careful here. pure functions can assume nothing is
- ag0aep6g (5/7) Feb 17 2018 That's only a problem when the called function is strongly pure, right?
- Steven Schveighoffer (4/13) Feb 17 2018 That's fine as long as you aren't going to use the allocator to make any...
- =?UTF-8?B?Tm9yZGzDtnc=?= (7/10) Feb 17 2018 I found a solution at
- Steven Schveighoffer (13/31) Feb 17 2018 Pure functions cannot access shared or thread-local data. They are not
- Eduard Staniloiu (15/24) Feb 17 2018 As the folks before me have pointed out, the language doesn't
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?
Feb 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'
Feb 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 :)
Feb 17 2018
On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote:I don't understand. I thought std.experimental.allocators API was designed to be able express these needs, andralex?in pure functions?pure means no globals. As in none :)
Feb 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: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.I don't understand. I thought std.experimental.allocators API was designed to be able express these needs, andralex?in pure functions?pure means no globals. As in none :)
Feb 17 2018
On Saturday, 17 February 2018 at 12:35:00 UTC, rikki cattermole wrote: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?in pure functions?pure means no globals. As in none :)
Feb 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.
Feb 17 2018
On 2/17/18 8:32 AM, ag0aep6g wrote:On 02/17/2018 01:35 PM, rikki cattermole wrote:You have to be a bit careful here. pure functions can assume nothing is happening and simply not call the function. -Stevepure 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.
Feb 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.
Feb 17 2018
On 2/17/18 9:54 AM, ag0aep6g wrote:On 02/17/2018 03:04 PM, Steven Schveighoffer wrote:That's fine as long as you aren't going to use the allocator to make any immutable or const data ;) -SteveYou 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.
Feb 17 2018
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!
Feb 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
Feb 17 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
Feb 17 2018