【问题标题】:Nim equivalent to python's `help()`Nim 相当于 python 的 `help()`
【发布时间】:2020-04-15 04:54:36
【问题描述】:

nim 是否编译文档字符串,以便我们可以在运行时回显它们?

类似:

>>> echo help(echo)
"Writes and flushes the parameters to the standard output.[...]"

【问题讨论】:

    标签: python documentation docstring nim-lang


    【解决方案1】:

    已编辑:有一种方法可以实现帮助功能

    事实证明,使用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

    【讨论】:

    • 嗯,所以我必须使用外部工具将它们放入我的 repl 项目(inim)?尼姆建议?这些插件和 ide 如何获取信息?
    • 是的,它是通过 nimsuggest。是的,要在 repl 中实现帮助,您可以调用 nimsuggest。
    • 这是相关文档:nim-lang.org/docs/nimsuggest.html
    猜你喜欢
    • 1970-01-01
    • 2019-09-19
    • 2020-12-04
    • 2013-11-14
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2016-05-08
    • 2011-06-05
    相关资源
    最近更新 更多