Skip to main content

Documenting your code

A comment is for whoever reads the code. A documentation comment goes further: IntelliJ shows it wherever the function is used.

Comments​

A comment starts with # and runs to the end of the line. Nylon has no block comments, so a comment of several lines starts each one with #.

# Half speed, so the robot doesn't tip over.
let cruise = 50

let stop = 0 # A motor at 0 doesn't turn.
Doesn't compile

// and /* aren't comments in Nylon. The build refuses them, and says to start a comment with #:

// Half speed.
let cruise = 50

Documentation comments​

A documentation comment opens with ##. It's the comment lines directly above a declaration, with no blank line between them and it.

##
# Scales a stick's reading to a motor's speed.
# @param value the stick's reading, from -1 to 1
# @returns the motor's speed, from -100 to 100
let speed(value: f32): i32 = i32(value * f32(100))
  • @param describes a parameter. Its first word is the parameter's name, and the rest says what it is.
  • @returns describes what the function gives back.
  • @{name} refers to a parameter in the text, as in the sum of @{left} and @{right}.

What IntelliJ shows​

Hover over a function that has a documentation comment, or put the cursor on it and open Quick Documentation (F1 on a Mac, Ctrl+Q on Windows and Linux). IntelliJ shows the function's declaration, the comment's text, and then its Params and Returns, with each parameter's name set as code.

In a library​

A library carries its documentation comments with it, so someone who adds your library sees them on hover too, as they see the ones in their own project. Write them on what your library offers, and the people using it don't need to open your code to learn what a function takes.