On 10/26/2011 07:01 PM, Jonathan M Davis wrote:
On Wednesday, October 26, 2011 03:04 bearophile wrote:
Jonathan M Davis:
On Wednesday, October 26, 2011 11:15:20 Gor Gyolchanyan wrote:
I see. But is there any practical advantage of a function being pure?
I mean, besides an optimization hint for the compiler, of course.

1. You know that it doesn't access global variables, which is at minimum
an advantage as far as understanding the code goes.

The lack of side effects makes it (maybe) less hard to understand code,
makes testing and unit testing simpler, and allows some optimizations,
like replacing filter(map()) with a map(filter())...

In DMD 2.056 several functions and higher order functions like array(),
map(), filter(), etc, aren't (always) pure, so I think D/Phobos purity
needs a bit of improvement. This compiles:

import std.algorithm;
void main() pure {
int[] a;
map!((int x){ return x; })(a);
map!((x){ return x; })(a);
}


This doesn't:

import std.algorithm, std.array;
void main() pure {
int[] a;
map!q{ a }(a);
filter!q{ a }(a);
array(a);
int[int] aa;
aa.byKey();
aa.byValue();
aa.keys;
aa.values;
aa.get(0, 0);
aa.rehash;
}

That's because the functions for associative arrays haven't been fixed up for
purity yet. It'll happen. The situation with pure has already improved
drastically over the last few releases.

- Jonathan M Davis

map!q{ a }(a); fails purity validation too.
That is possibly a compiler bug.


Reply via email to