Tools
Doc comments
Writing /// and //! docs in Markdown, their examples as tests, and the JSON fb doc writes from them.
A declaration’s docs are the /// lines right before it, in Markdown. A module’s are the //!
lines at the top of its file. Editors show them on hover, fb doc
writes them as JSON, and the registry shows a package’s.
//! Prices for a small shop: see [Price].
Import std.core
/// The price of `count` items at `each`, with one free for every ten.
///
/// # Examples
///
/// ```
/// Expect(Price(10, 5) == 45)
/// ```
Public Function Price(count: Int, each: Int): Int
Return (count - count / 10) * each
End FunctionWhere they go
- The
///lines right before a declaration, each on a line of its own, before its attributes, with no blank line between, are its doc comment. - A top-level Function, Struct, Class, Interface, Attribute, Enum, Global, Const, or Alias has one, and so do the members of a Struct, Class, Interface, or Extend, the Functions of an Extern block, and an Enum’s members.
- The
//!lines at the top of a file, before its first line of code, are its module’s. ////is an ordinary comment. A///comment on no declaration documents nothing, andfb docwarns about it.
What’s in one
A doc comment is Markdown, written as Rust’s are:
- The summary is its first sentence, which lists show: up to the first
.,!, or?outside code that a space follows, or that ends the first paragraph. - Sections start with headings:
# Errorsfor what it throws,# Panicsfor how it can be misused, and# Examples. - Parameters go in backticks, as
`count`. - Links go in brackets, like
[Price],[Stack.Push], or[std.collections.stack], and name a declaration as code where the comment is would: its type’s members, then its module’s declarations, those of the modules it imports, and the built-ins. Backticks and()can go inside, as in[`Push()`].fb docwarns about a link that names nothing.
A member without a doc that implements an Interface’s member, or overrides a base Class’s, has that one’s.
Examples are tests
The code blocks under # Examples, in a fence with no language or fb, are tests:
fb test compiles and runs each, named after its declaration, as
shop.prices.Price (example). So a doc’s examples can’t go stale.
- An example has
Import std.core,Import std.test, and its declaration’s module without writing them, and writes any other Import at its top. - A line that starts with
#is compiled but not shown, for setup a reader doesn’t need. - A fence of
fb no_runis only compiled, for code that would wait on a network or a person. - An example’s errors, failures, and stack traces name the doc comment’s lines.
fb doc’s JSON
{
"format": 1,
"package": "shop",
"version": "0.3.0",
"modules": [
{
"name": "shop.prices",
"file": "prices.fb",
"doc": { "summary": "...", "markdown": "...", "sections": [], "links": [], "examples": [] },
"items": []
}
]
}- A module has its
name, itsfilefrom the package’s folder, itsdoc, and itsitems. A module with no Public declarations and no doc is left out, and they’re sorted by name. - An item is a declaration users see:
kind(function, struct, class, interface, attribute, enum, alias, global, const, field, static field, method, static method, constructor, operator, property, enum member, or extend),name,path(as links name it, likeshop.prices.Price),signature(its first line, as written),attributes,visibility,fileandline,doc, and for a type, an Enum, or an Extend, itsmembers. A member with an inherited doc hasdoc_from, the path it came from. - A doc has its
summary; itsmarkdown, without examples’ hidden lines; itssections, each aheadingand itsmarkdown; itslinks, each thetextwritten and thepathit names, orbuiltinfor a built-in; and itsexamples, each one’scode, thelineof its fence, and whether itruns.
Report an issue with this page or with FutureBlitz on GitHub.