Compile-time int-to-string conversion

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Wed May 6 00:50:18 PDT 2009


How can one, in D2, at compile time, convert an integer value (or any 
other type, for that matter) to a string? Here's a simplified example of 
what I want to do:

     import std.conv;

     template Say(int N)
     {
         pragma(msg, to!string(N));
     }

     mixin Say!(123);

This doesn't compile, because the to!string() function for some reason 
can't be evaluated at compile time. (I've included the error messages 
below.)

In D1 I used to write N.stringof, which worked as expected, but in D2 
this would just print "N" instead of "123".

-Lars


Compile errors:
/usr/local/include/d/druntime/core/memory.di(162): Error: cannot 
evaluate gc_malloc(sz,ba) at compile time
/usr/local/include/d/phobos2/std/conv.d(2402): Error: cannot evaluate 
malloc(12u,2u) at compile time
/usr/local/include/d/phobos2/std/conv.d(2454): Error: cannot evaluate 
to(cast(uint)value) at compile time
tmp.d(5): Error: cannot evaluate to(123) at compile time
tmp.d(5): Error: pragma msg string expected for message, not 'to(123)'

tmp.d(8): Error: mixin tmp.Say!(123) error instantiating


More information about the Digitalmars-d-learn mailing list