Should GC.malloc be considered 'pure'?

bearophile bearophileHUGS at lycos.com
Thu Jun 9 15:28:00 PDT 2011


Andrei:

> 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.

So is the ptr field of a dynamic array allocated inside a strongly pure function.

Did you miss the discussion I've had here about the @trasparent? The allocation of heap memory can be considered pure if you use the allocated memory as a referentially transparent value (this means it's allowed to be used by reference, but your code is not allowed to read the value of the reference itself, so the future state of the code depends only on the contents of the referenced memory, and not the reference itself. Also, overwriting this reference is kosher still :-)

Bye,
bearophile


More information about the Digitalmars-d mailing list