简体   繁体   中英

Nim equivalent to python's `help()`

Does nim compile-in docstrings, so we can echo them at runtime?

Something like:

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

edited: there is a way to implement a help functionality

It turns out that using macros.getImpl it is possible to implement a functionality similar to the echo you report above ( playground ):

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.

the output is a bit wonky (a very large indent after first line of docstring) and it has some limitations: it will not work on some symbols - eg it works on toStrLit but not on getImpl; it will not work on overload. Those limitations might be improved in the future either with a better implementation or with fixes to compiler/stdlib.

previous answer

No, Nim will not compile docstrings ( edit : docstrings are not available at runtime, but they are part of the AST and they can be accessed at compile time, see above). With a supported editor you can get help hovering over identifiers or going to definition in source code.

For example in VS Code (with Nim extension), hovering over echo will give you:

在此处输入图像描述

And pressing F12 (on Windows) will go to definition of echo in systems.nim.

Another useful resource for standard library identifier is the searchable index .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM