Should GC.malloc be considered 'pure'?

Steven Schveighoffer schveiguy at yahoo.com
Thu Jun 9 13:13:29 PDT 2011


On Thu, 09 Jun 2011 16:08:33 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 6/9/11 12:51 PM, KennyTM~ wrote:
>> Given that the 'new' expression can be used in 'pure', should it be that
>> GC allocation functions like GC.malloc, GC.qalloc and GC.extend (?) be
>> weakly pure also? And should it apply to other managed allocators as
>> well, e.g. the proposed TempAlloc?
>>
>> I'm asking this as one of the specializations of std.conv.toImpl calls
>> GC.malloc, which is one of the 11 causes preventing std.conv.to from
>> being pure, and GC.qalloc and GC.extend (and memcpy and Array.capacity)
>> are used by std.array.appender (of pure range), and appender is also a
>> major reason why std.conv.to is not pure.
>
> GC.malloc is not technically pure because its result does not only  
> depend on arguments - it's a fresh value each time. So the compiler  
> can't apply pure reasoning to it.

It's weak-pure, not strong-pure.  weak-pure functions are allowed to  
return different values for two different runs with the same parameters.   
Essentially, weak-pure functions cannot have pure reasoning applied to  
them, *but* they can be called from strong-pure functions.

It's not technically even weak-pure because it uses global data.  But  
because memory allocation is so important to any functional programming,  
it needs to be an exception.

-Steve


More information about the Digitalmars-d mailing list