0atman
0atman

Reputation: 3385

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

Upvotes: 2

Views: 283

Answers (1)

pietroppeter
pietroppeter

Reputation: 1473

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 - e.g. 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:

enter image description here

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.

Upvotes: 3

Related Questions