【问题标题】:How to echo/print at compile time in Nim?如何在 Nim 的编译时回显/打印?
【发布时间】:2017-07-08 16:58:17
【问题描述】:

在处理编译时功能时,最好在编译时echo 一些东西。如果echo 带有宏,则它已在编译时执行。但是是否也可以在编译时打印一些东西,例如从全球范围?我正在寻找像echoStatic 这样的函数:

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

# some macro definitions

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

# more macro definitions

【问题讨论】:

  • 只是一个自我记录的问题,因为我一直忘记解决方案......

标签: compile-time nim-lang


【解决方案1】:

不需要特殊的echoStatic。这是通过running code at compile time的通用解决方案解决的,即使用static块:

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

# some macro definitions

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

# more macro definitions

【讨论】:

    【解决方案2】:

    在 C、C++ 和 D 等语言中,您通常可以使用 pragma 来完成这项工作。这也适用于 Nim:

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

    它还打印对编译时调试有用的文件、行和列。

    【讨论】:

      猜你喜欢
      • 2016-04-15
      • 1970-01-01
      • 2022-08-13
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      相关资源
      最近更新 更多