【发布时间】:2020-04-15 04:54:36
【问题描述】:
nim 是否编译文档字符串,以便我们可以在运行时回显它们?
类似:
>>> echo help(echo)
"Writes and flushes the parameters to the standard output.[...]"
【问题讨论】:
标签: python documentation docstring nim-lang
nim 是否编译文档字符串,以便我们可以在运行时回显它们?
类似:
>>> echo help(echo)
"Writes and flushes the parameters to the standard output.[...]"
【问题讨论】:
标签: python documentation docstring nim-lang
事实证明,使用macros.getImpl 可以实现类似于您上面报告的回声(playground)的功能:
import macros
macro implToStr*(ident: typed): string =
toStrLit(getImpl(ident))
template help*(ident: typed) =
echo implToStr(ident)
help(echo)
输出:
proc echo(x: varargs[typed, `$`]) {.magic: "Echo", tags: [WriteIOEffect],
gcsafe, locks: 0, sideEffect.}
## Writes and flushes the parameters to the standard output.
##
## Special built-in that takes a variable number of arguments. Each argument
## is converted to a string via ``$``, so it works for user-defined
## types that have an overloaded ``$`` operator.
## It is roughly equivalent to ``writeLine(stdout, x); flushFile(stdout)``, but
## available for the JavaScript target too.
##
## Unlike other IO operations this is guaranteed to be thread-safe as
## ``echo`` is very often used for debugging convenience. If you want to use
## ``echo`` inside a `proc without side effects
## <manual.html#pragmas-nosideeffect-pragma>`_ you can use `debugEcho
## <#debugEcho,varargs[typed,]>`_ instead.
输出有点不稳定(第一行文档字符串后缩进很大)并且它有一些限制:它不适用于某些符号 - 例如它适用于 toStrLit,但不适用于 getImpl;它不会在过载时工作。未来可能会通过更好的实现或对编译器/stdlib 的修复来改善这些限制。
不,Nim 不会编译文档字符串(edit:文档字符串在运行时不可用,但它们是 AST 的一部分,可以在编译时访问,见上文)。借助受支持的编辑器,您可以获得将鼠标悬停在标识符上或转到源代码中的定义的帮助。
例如在 VS Code(带有 Nim 扩展)中,将鼠标悬停在 echo 上会给您:
然后按F12(在 Windows 上)将转到 systems.nim 中的 echo 定义。
标准库标识符的另一个有用资源是searchable index。
【讨论】: