3 ms·
Does the requirement to pass around an allocator end up causing a "what color is your function" problem?
by daxfohl 3y ago
Does the requirement to pass around an allocator end up causing a "what color is your function" problem?
- seabrookmx 3y agoNo if anything it solves that. It's inversion of control.. the caller chooses the bahavior not the callee. Using async/await as an example, you have to have an async implementation (red) of a function and a non-async implementation (blue). In this case, the callee chooses the behavior. If instead you had a function that took some sort of runtime as a parameter that it could use to run downstream code synchronously or asynchronously, then you'd have something comparable to Zig's allocator. The red and blue functions would instead be one function where you pass a different runtime.
- bvrmn 3y agoAdding additional function argument is quite a stretch to be called as color change. For application code you could use global allocator though.
- deleted 3y ago[deleted]
- defen 3y agoFrom a syntactic perspective, no, because there is an Allocator vtable interface in the stdlib. Anything that implements alloc, resize, and free can be used interchangeably in any function that takes a `std.mem.Allocator` argument. You don't need to type different things on your keyboard in order to handle different allocators within a function. From a code organization perspective, slightly, only in the sense that must ensure that you free memory using the same allocator that allocated it.