Comments

It's useful to include comments with your code to help aid understanding both for other developers and your future self.

In Nim, there are two types of comments that you should be aware of: line comments, and doc comments.

Line comments are used to provide a simple comment. They begin with a single '#' character, and extend until the end of the line:

# Line comments are anything after '#', and extend to the end of the line

let foo = "bar" # They can also follow other code on a line

# For longer comments, you can feel free to use multiple lines
# Each line starts with the comment character

Doc comments are a special type of comment that are used for documentation purposes and are actually parsed by the compiler. This means that they can only occur in certain places - such as at the start of a file, after function or type definitions and a few other key places. The contents of doc comments are parsed as RST by the Nim DocGen tool to generate documentation. Doc comments use two '#' symbols:

proc add(x, y: int): int =
  ## This is a documentation comment for the `add` function.
  ## It can span multiple lines, with each line prefixed by `##`

Multiline Comments

It's possible to write multiline comments without preceeding every line with the '#' symbol, thanks to the multiline string literal and the discard statement:

discard """This is a multiline comment

Note that there's no need to prefix lines with '#'"""

Since Nim version 0.13.0, there is also built in support for multiline comments with a new special syntax combining the comment symbol with '[':

#[Comment here.
Multiple lines
are not a problem.]#

These multiline comments can also be nested, and the same syntax can be used for doc comments:

proc add(x, y: int): int =
  ##[This is a documentation comment for the `add` function.
  It can span multiple lines without any prefix
  ]##