Infer purity of functions too?

bearophile bearophileHUGS at lycos.com
Mon Jul 18 16:07:16 PDT 2011


> Example: if a not pure function sqr is called by both a not pure template bar and by a pure function foo, the compiler raises an error in foo, because sqr is not pure, but compiles the pure main because sqr called by bar is seen as pure :-)

This seems quite useful for delegates given to higher order functions:

int spam(in int x) pure { return x * 3; }
auto squares = map!((int x){ return spam(x) * x; })(iota(10));

With this idea squares is usable in a pure function too, because the map template infers the given delegate as pure.

Otherwise you need to write:

int spam(in int x) pure { return x * 3; }
auto squares = map!((int x) pure { return spam(x) * x; })(iota(10));

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list