How to echo/print at compile time in Nim?
Asked Answered
J

2

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
Joppa answered 19/2, 2017 at 12:28 Comment(1)
Just a self-documenting question, because I keep forgetting the solution...Joppa
J
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
Joppa answered 19/2, 2017 at 12:28 Comment(0)
D
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.

Destructible answered 28/11, 2021 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.