CTFE: What limitations remain?

bearophile bearophileHUGS at lycos.com
Mon Jul 25 08:20:25 PDT 2011


Don:

> The updated CTFE documentation is here:
> http://d-programming-language.org/function.html

> 3. the following statement types are not allowed:
> * with statements

I hope this 'with' limitation is temporary :-)

------------------------

Regarding that whole page:

I think the section about purity doesn't say that __ctfe is allowed in pure functions.

------------------------

>Cyclic functions (i.e. functions that wind up directly or indirectly calling themselves) are inferred as being impure, throwing, and @system.<

I didn't know/remember this. Is this done to simplify the analysis?


int fact(T)(in T n) {
    return n ? (n * fact(n-1)): 1;
}
void main() pure {
    auto x = fact(5);
}

------------------------

>Attribute inference is not done for other functions, even if the function body is present.<

In D.learn I have shown an idea: 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 impure function sqr() is called by both an impure function 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
}


But it seems a bit complex for the final programmer too (and maybe it needs a one level deeper purity inference).

Bye,
bearophile


More information about the Digitalmars-d mailing list