Memory allocation purity

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon May 19 01:37:15 PDT 2014


On Mon, 19 May 2014 07:37:55 +0000
via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> On Monday, 19 May 2014 at 06:30:46 UTC, Jonathan M Davis via
> Digitalmars-d wrote:
> > makes dealing with immutable far, far more pleasant. It's
> > particularly useful
> > when you need to allocate an immutable object but also need to
> > mutate it as
> > part of initializing it. If you do it in a pure function where
> > the compiler
> > knows that the object couldn't have been passed in, then the
> > return type can
> > be freely converted to various levels of mutability - including
> > immutable -
> > without having to use immutable within the function.
>
> It does not appear as a clean design that functions should have
> different semantics than a block. What matters is that the object
> reference is unique. Confusing this with pure seems like a bad
> idea.

I don't follow you. The fact that D's pure helps the compiler determine cases
where it knows that the return value of a function is unique is a key feature
of pure and has proven to be a great idea. Perhaps you're hung up on the fact
that the term "pure" is being used, and you're thinking about functional
purity? If so, forget about pure in the functional sense if you want to
discuss D purity. You need to think of it as something more like @noglobal.
That combined with other information in the function signature allows the
compiler to determine cases where it knows that the returned value is unique.
It also can lead to the compiler determining that a function in functionally
pure and thus memoizable, but at this point, that's pretty incidental to what
pure is and does. It's part of it, but it's not the primary feature of what
D's pure is or is for.  It's unfortunate that the language's evolution lead us
to using the term pure for what it's currently used for, but we're pretty much
stuck with it at this point. Regardless, the fact that D's pure allows us to
determine when the return value of a function has to be unique is fantastic
and has proven very helpful.

- Jonathan M Davis


More information about the Digitalmars-d mailing list