D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2066 - to!(string)(int) into CTFE-compatible
Summary: to!(string)(int) into CTFE-compatible
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 major
Assignee: Andrei Alexandrescu
URL:
Keywords:
Depends on: 3556
Blocks:
  Show dependency treegraph
 
Reported: 2008-05-04 15:59 UTC by Simen Kjaeraas
Modified: 2015-06-09 01:21 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2008-05-04 15:59:04 UTC
import std.string;

pragma(msg, toString(42));

The above program fails to compile, with the error message "cannot evaluate toString(42) at compile time"
Comment 1 Nazo Humei 2008-05-04 16:20:31 UTC
Reply to d-bugmail@puremagic.com,

> http://d.puremagic.com/issues/show_bug.cgi?id=2066
> 
> Summary: toString(int) nto CTFE-compatible
> Product: D
> Version: 2.014
> Platform: PC
> OS/Version: Windows
> Status: NEW
> Severity: major
> Priority: P2
> Component: Phobos
> AssignedTo: bugzilla@digitalmars.com
> ReportedBy: simen.kjaras@gmail.com
> import std.string;
> 
> pragma(msg, toString(42));
> 
> The above program fails to compile, with the error message "cannot
> evaluate toString(42) at compile time"
> 

in "static code" you can use "42.stringof" but, IIRC, that won't work in 
other CTFE functions.


Comment 2 Simen Kjaeraas 2008-05-04 16:32:13 UTC
(In reply to comment #1)
> Reply to d-bugmail@puremagic.com,
> in "static code" you can use "42.stringof" but, IIRC, that won't work in 
> other CTFE functions.

Example:

template foo(int line = __LINE__)
{
  pragma(msg, line.stringof); // prints 'line'.
  pragma(msg, toString(line)); // should print a number of some kind. Currently does not.
}

That is pretty much why I need this.
Comment 3 Janice Caron 2008-05-04 16:58:12 UTC
On 04/05/2008, d-bugmail@puremagic.com <d-bugmail@puremagic.com> wrote:
>  import std.string;
>
>  pragma(msg, toString(42));
>
>  The above program fails to compile, with the error message "cannot evaluate
>  toString(42) at compile time"

You could try

    import std.metastrings;

    pragma(msg, ToString!(42));

Comment 4 Ross Canning 2008-11-24 00:23:47 UTC
Thanks for the workaround, Janice. Here is another example that is almost certainly related (CTFE seems to work only with single-digit numbers!):

import std.stdio;
import std.string;

template Foo(uint id) {
   invariant string Foo = "writefln(" ~ std.string.toString(id) ~ ");";
}

invariant uint ID = 1;    // THIS LINE WORKS
//invariant uint ID = 10;     // THIS LINE FAILS

void main() {
   mixin(Foo!(ID));
}
Comment 5 Don 2010-01-18 00:59:38 UTC
Changed bug title to reflect changes in Phobos: toString -> to!(string).
Fixed in svn 1402. Requires DMD svn 332 or later.
Comment 6 Walter Bright 2010-01-30 22:44:16 UTC
fixed dmd 2.040