Forcing compile time evaluation of pure functions

Simen Kjaeraas simen.kjaras at gmail.com
Thu Jun 30 06:53:08 PDT 2011


On Thu, 30 Jun 2011 07:11:44 +0200, scarrow <shawn.baird at gmail.com> wrote:

> 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.

Actually, there might. Pure functions are allowed to depend upon the
immutable global variables, whose values may be calculated at startup
(see static this)

-- 
   Simen


More information about the Digitalmars-d-learn mailing list