[Issue 3835] [CTFE] Fragile CTFE

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 19 04:24:34 PST 2010


http://d.puremagic.com/issues/show_bug.cgi?id=3835



--- Comment #2 from bearophile_hugs at eml.cc 2010-02-19 04:24:33 PST ---
> Please put in an example that it wouldn't compile!

OK. This two versions of genFactorials print the wrong results:
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0


import std.stdio: writeln;

int factorial(int n) {
    if (n == 0)
       return 1;
    return n * factorial(n - 1);
}

int[] genFactorialsA(int n) {
    int[] result = new int[n + 1];
    foreach (i, ref el; result)
        el = factorial(i);
    return result;
}

int[N] genFactorialsB(int N)() {
    int[N] result;
    foreach (i, ref el; result)
        el = factorial(i);
    return result;
}

enum int N = 13;
static enum auto factorials1 = cast(int[N])genFactorialsA(N - 1);
enum auto factorials2 = genFactorialsB!13;
void main() {
    writeln(factorials1);
    writeln(factorials2);
}


This version doesn't compile, the compiler raises:
test7b.d(7): Error: pure function 'factorial' cannot call impure function
'factorial'

test7b.d(12): Error: pure function 'genFactorials' cannot call impure function
'factorial'

test7b.d(17): Error: cannot evaluate genFactorials(12) at compile time
test7b.d(17): Error: cannot evaluate genFactorials(12) at compile time



import std.stdio: writeln;

pure int[] genFactorials(int n) {
    pure static int factorial(int n) {
        if (n == 0)
           return 1;
        return n * factorial(n - 1);
    }

    int[] result = new int[n + 1];
    foreach (i; 0 .. n+1)
        result[i] = factorial(i);
    return result;
}

enum int N = 13;
static enum auto factorials = cast(int[N])genFactorials(N - 1);
void main() {
    writeln(factorials);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list