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;
}
Bye,
bearophile