On Saturday, 18 July 2015 at 13:48:20 UTC, Clayton wrote:
Am new to D programming, am considering it since it supports compile-time function execution . My challenge is how can I re-implement the function below so that it is fully executed in compile-time. The function should result to tabel1 being computed at compile-time. There seems to be a lot of mutation happening here yet I have heard no mutation should take place in meta-programming as it subscribes to functional programming paradigm.



void computeAtCompileTime( ref string pattern ,ref int[char] tabel1){
        int size = to!int(pattern.length) ;
        
        foreach( c; ALPHABET){
                tabel1[c] = size;
        }
        
        for( int i=0;i<size -1 ; ++i){   //Initialise array
                tabel1[pattern[i]] = size -i-1;

pragma(msg, format("reached pattern table1[pattern[i]]=(%s) here", table1[pattern[i]].stringof ~" v="~ (size -i-1).stringof));
        }

        
        
        
}

Actually, the main things you can't do in CTFE are FPU math operations (much of std.math has issues unfortunately), compiler intrinsics, pointer/union operations, and I/O. I don't immediately see anything that will cause issues with CTFE in that function. However, sometimes the compiler isn't smart enough to figure out that it should be doing that, but you can force the compiler to try CTFE using this pattern

int ctfeFunc() {
}

void main() {
    enum val = ctfeFunc();

}

enums are manifest constants, and thus must be computable at compile time, so this will issue an error if something in your function can't CTFE.

Reply via email to