[dmd-beta] beta 2
Jonathan M Davis
jmdavisProg at gmx.com
Wed Jul 6 13:29:27 PDT 2011
On 2011-07-06 12:50, Jonathan M Davis wrote:
> On 2011-07-06 12:46, Walter Bright wrote:
> > On 7/6/2011 12:08 PM, David Simcha wrote:
> > > Again, where does this leave the weakly pure function issue? I want to
> > > be sure that weak purity for non-const member functions is going away
> > > forever before I take it out of all my code and test this beta in any
> > > detail.
> >
> > The trouble was that there were a lot of functions marked as 'pure' that
> > were changing things pointed to by its arguments.
> >
> > I understand that within a pure function, such a pure function could
> > modify locals without the caller becoming impure. But those functions
> > are still impure.
>
> But they're supposed to be weakly pure, because they don't alter an global
> or static variables. They're _supposed_ to be able to alter stuff which is
> passed to them. They're just weakly pure instead of strongly pure and thus
> can't be optimized out. Then strongly pure functions can call them and
> retain all of their guarantees. It sounds like you're throwing out the
> whole idea of weak purity. Certainly, as it stands, weak purity is pretty
> thoroughly trashed if not outright gone.
Correction, they're weakly pure because they don't access global or static
variables which can be mutated (so they can access them if they're immutable
or if they're const value types but otherwise can't access them and still be
pure).
As I understand it, this is how purity works:
1. A function can be pure if it doesn't can any functions which are not pure
and if it doesn't access any global or static variables which can be mutated
by anything over the course of the program (so it can access global and static
variables which are immutable or which are const value types). It is perfectly
legal for a pure function to alter its arguments as long as they're not const
or immutable.
2. A function is strongly pure if it can be determined at the call site that
it is guaranteed not to alter any of its arguments. At present, that's
restricted to functions whose parameters are all either immutable or
implicitly convertible to immutable, but there are cases where we could extend
that to work with const parameters.
3. A function which is not strongly pure is weakly pure.
4. Calls to strongly pure functions can be optimized out such that if more
than one call to a strongly pure function is made within a statement, then the
function is called only once, and its result is used for each other instance
within the statement where the function would have been called. The
optimization could probably be extended further than a single statement under
at least some circumstances, but I don't believe that it's currently
implemented that way.
5. Calls to weakly pure functions cannot be optimized out, but weakly pure
functions can still be called from within strongly pure functions, so it's
highly valuable to be able to be able to make functions weakly pure.
The recent purity changes seem to be throwing weakly pure out the window,
which is a huge blow to purity in general.
- Jonathan M Davis
More information about the dmd-beta
mailing list