Forcing compile time evaluation of pure functions

scarrow shawn.baird at gmail.com
Wed Jun 29 22:11:44 PDT 2011


Hey all,

I'd like to embed hashed strings into my code.  The C++ version of this engine
ran an external tool to preprocess the files.  In D my strongly pure function
is only evaluated if I assign it to something like an enum or invoke it from a
template.  So the following generate compile time hashes:

    enum blort = Hash("foo");
    f(CHash!("foo"));

Annoyingly, however, I can't do the following at compile time:

    f(Hash("foo"));

I'm not sure what the point is in distinguishing between these two cases.  If
it is a properly pure function there should be no harm in doing a compile time
evaluation.  Ideally (and maybe this is being purposely avoided) I want to be
able to write:

    string s = "bar";
    f(Hash("foo"));    // invoke the compile time version and pass a constant to f
    f(Hash(s));    // invoke the runtime version and pass the result to f


More information about the Digitalmars-d-learn mailing list