[dmd-beta] beta 2

Jonathan M Davis jmdavisProg at gmx.com
Wed Jul 6 15:14:12 PDT 2011

On 2011-07-06 15:02, Walter Bright wrote:
> On 7/6/2011 2:12 PM, David Simcha wrote:
> > On Wed, Jul 6, 2011 at 5:09 PM, Walter Bright <walter at digitalmars.com
> > 
> > <mailto:walter at digitalmars.com>> wrote:
> > This is what I have difficulty with. Consider:
> > pure void foo(int* p) { *p = 3; }
> > 
> > That isn't pure, or weakly pure.
> > 
> > ???? Yes it is. It can be called from a strongly pure function without
> > violating purity
> Yes, but it is not pure itself.

Yes it is. It's weakly pure. Sure, it can't be optimized out on its own, but 
no weakly pure function can be. It's pure because it does not directly access 
any global or static variables which can be mutated. Sure, p _could_ be passed 
a global or static variable, but it doesn't have to be, and it's guaranteed 
that it isn't when it's called from a strongly pure function (or is in the 
call chain of a strongly pure function).

With weak purity, all that pure really means is that if the function is in the 
call chain of a strongly pure function, it cannot access a global or static 
variable which can be mutated and that it calls no functions which are not 
themselves pure. It's only when a function is strongly pure that 
optimizability and the more traditional meaning of purity comes in.

A weakly pure function is a function that when called from a strongly pure 
will not violate the purity of the strongly pure function. foo would not 
violate the purity of any strongly pure function which called it. And all 
weakly and strongly pure functions are pure in that they're marked with the 
pure modifier (or have it implicitly due to inference by the compiler). So, 
yes foo _is_ pure. It's _weakly_ pure, but it's pure.

- Jonathan M Davis

More information about the dmd-beta mailing list