Can't read a constant value in compile time?

Steven Schveighoffer schveiguy at gmail.com
Tue Nov 20 19:18:43 UTC 2018


On 11/20/18 1:54 PM, Adnan wrote:
> Godbolt: https://godbolt.org/z/SWWOu7
> 
> When I write `something!(aNumber)()` and if a number is an 
> immutable/enum it should be able to be read at compile time, right? Why 
> is this different?
> 
> auto fizzbuzz(uint N)() {
>      static string accumulate;

string is not immutable. It's data is immutable, but the array itself 
can be changed. So it's not readable at compile time. But in any case, 
you still aren't trying to read it at compile time (i.e. accumulate is 
not passed into another template as a template parameter).

>      return fizzbuzz!N(accumulate);
> }
> 
> auto fizzbuzz(uint N)(ref string result) if (N % 3 && N % 5) {
>      import std.conv : to;
> 
>      result ~= N.to!string ~ "\n";
>      return fizzbuzz!(N - 1)(result);
> }
> 
> auto fizzbuzz(uint N)(ref string result) if (!(N % 15)) {
>      result ~= "FizzBuzz\n";
>      return fizzbuzz!(N - 1)(result);
> }
> 
> auto fizzbuzz(uint N)(ref string result) if (!(N % 3) && N % 5) {
>      result ~= "Fizz\n";
>      return fizzbuzz!(N - 1)(result);
> }
> 
> auto fizzbuzz(uint N)(ref string result) if (!(N % 5) && N % 3) {
>      result ~= "Buzz\n";
>      return fizzbuzz!(N - 1)(result);
> }
> 
> auto fizzbuzz(uint N : 0)(ref string result) {
>      return result;
> }
> 
> void main() {
>      import std.stdio : writeln;
> 
>      enum lmao = fizzbuzz!50();

Here you are trying to *execute* fizzbuzz at compile time. So it tries 
to interpret the runtime function at compile time (which should work).

However, static data doesn't exist in CTFE I don't think, so that's why 
it works when you remove static.

Why is it generating so much code? Because you are using templates, and 
even templates you only use at compile time STILL generate and get 
included in the binary (something I think D could probably fix).

Instead of using templates, just write it as runtime functions, and then do:

enum lmao = fizzbuzz(50);

It will go much better.

-Steve


More information about the Digitalmars-d-learn mailing list