Comments are useful summaries of line or block code that help developers better understand the code.

It improves the maintenance of the code and is easy to understand by other developers.

Comments text is ignored by the compiler and it does not generate output byte code.

A Nim language supports various comment types.

  • Single-line and inline comments
  • Multiline or block line comments
  • Documentation comments.

How to write a single-line comment in Nim?

Single-line comments are added as a new line or inline to code.

Single-line Comments always start with a double forward slash # symbol followed by a text string.

Syntax:

# Single-line comment example

Here is a Nim single-line comment example

// Hello World Example
  Echo "Comment example"; // Print text string to console
}

Nim block-level comments

Comments text does span in multiple lines, These are also called block-level comments.

These comments start with #[ and followed by the comment text in multiple lines, ends with ]#. Here is a syntax example

#[multi-line comment line1
multi-line comment line2
  multi-line comment line3
]#

Here is an example

/*
*
* Helloworld.nim file
*/

#[Hello world example code
Helloworld.nim file
]#
echo "Multiline comment example"
}

How to write and parse documentation comments in Nim?

Every program provides documentation for API and an end-user guide for understanding the code.

Nim provides special comments syntax for writing documentation comments in the code using double hash## and @ for tag syntax.

It provides rich documentation tags for functions, classes, and interfaces using the @ syntax format.

nim doc reads nim source code and generates Documentation output in different formats such as HTML, JSON, and latex.

It helps the user to develop documentation.

Here is a documentation comments example

## __isEmpty__ method.
## Checks for empty or null for a given string.
## Examples
##
##
##
## parameters string The given input string
## @returns A boolean The true or false values.


proc isEmpty(string:String ) {

}

Once you have the comments ready, we have to run the following command.

A:\work\nim>nim doc filename.nim

doc is a command used to generate the documentation in HTML output.

It generates documentation and outputs HTML files in the current folder.