Improvement in pure functions specification

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 21 12:15:43 PST 2016


On Wednesday, December 21, 2016 15:49:35 Stefan Koch via Digitalmars-d 
wrote:
> On Wednesday, 21 December 2016 at 15:40:42 UTC, Andrei
>
> Alexandrescu wrote:
> > On 12/20/2016 05:49 PM, Andrei Alexandrescu wrote:
> >> https://github.com/dlang/dlang.org/pull/1528 -- Andrei
> >
> > Dropped the void functions. On to the next scandal:
> >>A function that accepts only parameters without mutable
> >>indirections and
> >>returns a result that has mutable indirections is called a $(I
> >>pure factory
> >>function). An implementation may assume that all mutable memory
> >>returned by
> >>the call is not referenced by any other part of the program,
> >>i.e. it is
> >>newly allocated by the function.
> >>
> > Andrei
>
> Couldn't this be folded into :
> "The implementation may not remove a call to a pure function if
> does allocate memory ?"
>
> Since there is the concept of weakly pure functions the compiler
> cannot decide to remove functions on signature alone.
> Meaning the body has to be available for it to even attempt to
> elide the call.
>
> Therefore specifying implementation behavior based on the
> function signature is misleading IMO.

Why would the function body need to be there to elide the call? Only calls
to "strongly" pure functions can be elided when called multiple times, so
"weak" purity doesn't enter into the equation. And how "strong" a pure
function is has everything to do with its signature and nothing to do with
its body. pure has always been designed with the idea that it would be the
function signature that mattered. The body only comes into play when
inferring purity.

What Andrei has put here is to codify what the compiler needs to look at to
determine whether a strongly pure function may have allocated and returned
that memory (or something that referred to that memory) and made it so that
the compiler is not allowed to elide the call in that specific case. There
is no need to specifically mention memory allocation unless you're looking
to indicate why the spec is saying that such calls cannot be elided.

- Jonathan M Davis



More information about the Digitalmars-d mailing list