bluenote10
bluenote10

Reputation: 26710

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

Upvotes: 6

Views: 943

Answers (2)

ChrisoLosoph
ChrisoLosoph

Reputation: 607

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.

Upvotes: 1

bluenote10
bluenote10

Reputation: 26710

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

Upvotes: 9

Related Questions