6

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
bluenote10
  • 23,414
  • 14
  • 122
  • 178

2 Answers2

9

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
Community
  • 1
  • 1
bluenote10
  • 23,414
  • 14
  • 122
  • 178
1

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.

ChrisoLosoph
  • 459
  • 4
  • 8