staticnim-lang

Nim static compilation


I'm totally new to Nim.

Does Nim automatically try to do everything it can during compilation? The documentation talks a lot about figuring static stuff out at compile-time, and it seems this is a guiding philosophy of the language.

I have a program in python which computes some stuff by going through a bunch of nested loops, generating an array each loop, and then computing a heuristic based on the values in the array. There is no I/O during the run except at the end, where it writes the heuristic to a file or console.

The output is 100% deterministic and in principal knowable before the program runs, but Python computes the output at runtime of course, and this can take a long time, even after enabling multi-processing (since the problem is CPU and memory-bound).

If I were to rewrite the Python program in Nim, is the compiler smart enough to figure out the output at compile time? The runtime would thus just write the final output to a file.

I can simplify the Python program in Nim as:

var x: int
for i in 1..10:
    x += i
echo x

Returns:

55

Here the almost unreadable C output has a nimAddInt(...) call inside the NimMainModule, so I'm guessing in this particular case the action happens at runtime. How can I enforce this to go at compile time? Macros?


Solution

  • Nim won't automatically do things on compile time. You have to explicitly tell it to. Fortunately this is as easy as using the result in a compile-time context. For example by assigning it to a const or explicitly putting everything in a static block.