Infer purity of functions too?

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


Jonathan M Davis:

> And if pure were inferred for a function and then it became 
> impure, that could break a _lot_ of code.

OK. The restricted idea then is to infer only the purity of functions called by templates, to allow more templates to be pure, and such inferred purity is seen by function templates only.

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 :-)


int sqr(in int x) {
    return x * x;
}
int foo(in int x) pure { // error, sqr is not tagged pure
    return sqr(x) + sqr(x);
}
int bar(T)(in T x) {
    return sqr(x) * sqr(x);
}
void main() pure {
    bar(1); // OK, sqr can be inferred as pure
}


It looks a bit complex :-)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list