Comments & Documentation

Comments & Documentation

Comments

// starts a comment that runs to the end of the line. /* starts a block comment that runs to the next */.

// a line comment
var n = 0   // after code

/* a block comment
   over several lines */

Documentation Comments

A documentation comment describes the declaration that follows it. It is written as /// lines, or as a /** ... */ block:

/// Returns the index of the first element equal to `x`.
fn find(const v: [i32], x: i32) -> usize? { ... }

/**
 * A growable buffer of bytes.
 */
class Buffer { ... }

///< after a field or an enumerator documents the declaration in front of it:

enum Mode {
    Read,   ///< open for reading
    Write,  ///< open for writing
}

Format

The text of a documentation comment is Markdown. Tools that show documentation, such as the language server's hover, render it as written.

The first paragraph is the summary. It says what the declaration is or does, in a sentence or two. A blank /// line ends it; everything after is the detail.

/// Splits `s` at each occurrence of `sep`.
///
/// Adjacent separators produce empty pieces. An empty `sep` splits between
/// every character.
///
/// ### Parameters
/// - `s`: the text to split
/// - `sep`: the separator
///
/// ### Returns
/// The pieces, in order, as views into `s`.
///
/// ### Panics
/// `OutOfMemory` if the result cannot be allocated.
///
/// ### Example
/// ```kairo
/// var parts = split("a,b,,c", ",")   // ["a", "b", "", "c"]
/// ```
fn split(const s: string, const sep: string) -> [string] { ... }

The sections are third-level headings, in this order, each only when it has something to say:

HeadingContent
### Type parametersone list item per type parameter: - `T`: ...
### Parametersone list item per parameter: - `name`: ...
### Returnswhat the value is
### Panicseach panic type and when it is raised
### Examplea fenced kairo block

Shorter remarks stay in the detail as emphasis: **Note:**, **Warning:**, **Precondition:**, **Postcondition:**, **Invariant:**. Names from the code are written in backticks, and See followed by a name in backticks points at a related declaration.

/// Removes and returns the last element.
///
/// **Precondition:** the vector is not empty.
///
/// See `pop`.
fn pop_unchecked(self) -> T { ... }

A declaration whose name and signature already say everything needs only the summary line, or no comment at all.