GC.malloc is pure - wat

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 27 12:46:49 PDT 2015


On Monday, 27 April 2015 at 10:50:12 UTC, Steven Schveighoffer 
wrote:
> On 4/25/15 11:06 PM, deadalnix wrote:
>> On Friday, 24 April 2015 at 23:27:36 UTC, Steven Schveighoffer 
>> wrote:
>>>> pure function can access global immutable state that wasn't 
>>>> passed to
>>>> it, so you may want to revise your definition.
>>>
>>> Sure: s/accessing/altering, my mistake.
>>>
>>
>> That is the whole point. See it as follow: GC.malloc create 
>> new state.
>> As long as this new state doesn't escape the pure function, it 
>> is as if
>> that state was local.
>
> I get that, you can apply the same thing for free. No reason 
> this can't be a pure function:
>

With malloc, you have 2 new thing is userland:
  - A new state, which, as for the GC case could be considered 
pure.
  - A global state that you have to maintain (the fact that you 
allocated and must free) and this should not be pure.

> void foo()
> {
>    int *x = cast(int *)malloc(sizeof(int));
>    *x = 0;
>    scope(exit) free(x);
> }
>
> But there are other problems, as I pointed out in another post.
>
> -Steve

In that specific use case, I think this is fair to consider that 
code pure, or at least pretend it is. D being a system 
programming language, you can pretend this is pure.


More information about the Digitalmars-d mailing list