On 7/18/2011 7:00 PM, bearophile wrote:
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
I thought purity inference only worked one level deep. It would look at
each of the functions it calls. If they are all explicitly marked pure,
then its pure. I don't believe it analyzes the body of sqr to mark it as
pure. You'd still have to mark sqr as pure.