[英]Nim equivalent to python's `help()`
nim 是否会编译文档字符串,以便我们可以在运行时回显它们?
就像是:
>>> echo help(echo)
"Writes and flushes the parameters to the standard output.[...]"
事实证明,使用macros.getImpl
可以实现类似于您在上面报告的回声(游乐场)的功能:
import macros
macro implToStr*(ident: typed): string =
toStrLit(getImpl(ident))
template help*(ident: typed) =
echo implToStr(ident)
help(echo)
output:
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.
output 有点不稳定(第一行文档字符串后缩进很大)并且它有一些限制:它不适用于某些符号 - 例如它适用于 toStrLit 但不适用于 getImpl; 它不会在过载时工作。 将来可能会通过更好的实现或对编译器/stdlib 的修复来改善这些限制。
不,Nim 不会编译文档字符串(编辑:文档字符串在运行时不可用,但它们是 AST 的一部分,可以在编译时访问,见上文)。 使用受支持的编辑器,您可以获得将鼠标悬停在标识符上或转到源代码中的定义的帮助。
例如在 VS Code(带有 Nim 扩展)中,将鼠标悬停在 echo 上会给你:
并按F12
(在 Windows 上)将 go 定义为 systems.nim 中的回声。
标准库标识符的另一个有用资源是可搜索索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.