Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo/print at compile time in Nim?

When working on compile time features it would be nice to echo something at compile time. If an echo is withing a macro it is already executed at compile time. But is it also possible to print something at compile time e.g. from the global scope? I'm looking for a function like echoStatic in this:

echoStatic "Compiling 1. set of macros..."

# some macro definitions

echoStatic "Compiling 2. set of macros..."

# more macro definitions
like image 211
bluenote10 Avatar asked Feb 19 '17 12:02

bluenote10


2 Answers

There is no need for a special echoStatic. This is solved by the general solution of running code at compile time, which is to use a static block:

static:
  echo "Compiling 1. set of macros..."

# some macro definitions

static:
  echo "Compiling 2. set of macros..."

# more macro definitions
like image 62
bluenote10 Avatar answered Nov 15 '22 09:11

bluenote10


In languages like C, C++ and D, you can typically use a pragma for this job. This also works for Nim:

from strformat import `&`

const x = 3
{. hint: &"{$typeof(x)} x = {x}" .}  # <file location> Hint: int x = 3

It also prints the file, the line and the column which can be useful for compile-time debugging.

like image 33
ChrisoLosoph Avatar answered Nov 15 '22 10:11

ChrisoLosoph