Purity

Simen kjaeraas simen.kjaras at gmail.com
Sat Dec 18 03:30:31 PST 2010


spir <denis.spir at gmail.com> wrote:

> -1- How can this even compile?
> 	pure append (ref int[] a, int i) {a ~= i;}
> The _only_ task of this func is to change state.

It is designed to be usable in strongly pure functions. A strongly pure
function may call this weakly pure function without compromising its
purity. This because the strongly pure function could only pass the
weakly pure function its own local state, and changes would thus be
limited to the strongly pure function's scope.


> -2- Why is output writing considered an impure task? It has no influence  
> on the rest/future of the program, lets reasoning easy, does not touch  
> state.

Output would be affected by pure optimizations:

pure int writeStuff() {
     output( "Hi!" );
     return 1;
}

void foo( ) {
     int a = writeStuff( ) + writeStuff( );
}

Normally, the compiler could rewrite to
     int a = 2 * writeStuff( );
but that would fuck up output.

-- 
Simen


More information about the Digitalmars-d mailing list