[dmd-beta] beta 2

Walter Bright walter at digitalmars.com
Wed Jul 6 14:09:09 PDT 2011

On 7/6/2011 1:29 PM, Jonathan M Davis wrote:
> On 2011-07-06 12:50, Jonathan M Davis wrote:
>> 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.

This is what I have difficulty with. Consider:

     pure void foo(int* p) { *p = 3; }

That isn't pure, or weakly pure.

> 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
> _______________________________________________
> dmd-beta mailing list
> dmd-beta at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-beta

More information about the dmd-beta mailing list