If you could make any changes to D, what would they look like?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Oct 29 16:08:10 UTC 2021

On Fri, Oct 29, 2021 at 03:51:05PM +0000, Patrick Schluter via Digitalmars-d wrote:
> On Thursday, 28 October 2021 at 22:48:10 UTC, Dukc wrote:
> > On Thursday, 28 October 2021 at 19:46:32 UTC, Patrick Schluter wrote:
> > > Not at all. free cannot, by its semantic, be pure (same for
> > > malloc).  Trying to make free pure is a silly challenge.
> > 
> > https://dlang.org/phobos/core_memory.html#.pureFree
> I don't understand it. It does not make any sense. pure functions are
> function that do not depend on global state for their result. Weak
> purity allows for some exception like for a print function which has
> global effects but these effects have no feedback and can ne ignored.

Actually, even that is wrong. I/O changes global state, and as such have
no place in pure functions, not even weakly pure ones.  Weakly pure
functions may only modify *state they receive via their parameters*, and
nothing else.  Modifying any other state violates the definition of
(weak) purity and breaks any purity-based optimizations.

The only I/O permitted in pure functions, whether weakly pure or
strongly pure, are in debug statements, and that's only for the purposes
of debugging.  It should definitely not be something program semantics
depend on.

> This is not the case with allocation/free, which are, by defintion,
> dependend on a global state (even if only thread  local).

Yeah, pureFree makes no sense at all. It's a disaster waiting to happen.

> Each call to malloc, by definition must return another value and/or
> can return a same value with other parameter. The result does not
> depend on ANYTHING in its scope.
> pure allocation/free is a recipe for disaster.
> My understanding of purity at l east.

I agree. I don't know when/why pureFree was introduced, but it's all
kinds of wrong.

One may argue that pure functions may return allocated memory (this is
explicitly allowed in the specs), but IMO this is only allowed if the
allocation is *implicit*, i.e., part of a language construct, such that
its effects are not visible within the domain of language constructs.
Otherwise this opens the door to all kinds of subtle bugs that basically
make purity a laughably useless construct.


Computerese Irregular Verb Conjugation: I have preferences.  You have biases.  He/She has prejudices. -- Gene Wirchenko

More information about the Digitalmars-d mailing list