簡體   English   中英

Nim 相當於 python 的 `help()`

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM