GC.malloc is pure - wat

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 24 17:57:14 PDT 2015


On 4/24/15 8:30 PM, weaselcat wrote:
> On Friday, 24 April 2015 at 23:27:36 UTC, Steven Schveighoffer wrote:
>> All I'm saying is that GC.malloc alters global state. I agree that
>> it's OK to pretend that it doesn't because as long as you agree not to
>> base things on this knowledge, you are fine calling pure functions
>> that use GC.
>
> A lie that is universally agreed upon is virtually indistinguishable
> from the truth.
>
> the reasons against making malloc/free pure are very... thin.
>
> Walter himself attempted to make them pure only last week(!)
> https://github.com/D-Programming-Language/druntime/pull/1221

I hadn't noticed that.

And linked from there is the reason I was shown that free cannot be pure 
(from a while ago):

void freeIt(immutable(int)* x) pure { free(cast(int *)x); }

A compiler could legally just not even call this function.

It's a pretty compelling reason.

I have a feeling that we can't make free generally pure. When needed, it 
will have to be hacked somehow in a controlled way.

Or we could make some rule that you should never cast immutable pointers 
to mutable in order to free them. Doesn't sound very enforceable...

-Steve


More information about the Digitalmars-d mailing list