XML Documentation - F# (original) (raw)

You can produce documentation from triple-slash (///) code comments in F#. XML comments can precede declarations in code files (.fs) or signature (.fsi) files.

XML documentation comments are a special kind of comment, added above the definition of any user-defined type or member. They are special because they can be processed by the compiler to generate an XML documentation file at compile time. The compiler-generated XML file can be distributed alongside your .NET assembly so that IDEs can use tooltips to show quick information about types or members. Additionally, the XML file can be run through tools like fsdocs to generate API reference websites.

By default, XML documentation comments are ignored by the compiler. To change this, set --warnon:3390. The compiler will then verify the syntax of the XML and the parameters referred to in <param> and <paramref> tags.

You can generate the XML file at compile time by doing one of the following:

<GenerateDocumentationFile>true</GenerateDocumentationFile>  

For more information, see GenerateDocumentationFile property.

There are two ways to write XML documentation comments: with and without XML tags. Both use triple-slash comments.

If a /// comment does not start with a <, then the entire comment text is taken as the summary documentation for the code construct that immediately follows. Use this method when you want to write only a brief summary for each construct.

The comment is encoded to XML during documentation preparation, so characters such as <, >, and & need not be escaped. If you don't specify a summary tag explicitly, you should not specify other tags, such as param or returns tags.

The following example shows the alternative method, without XML tags. In this example, the entire text in the comment is considered a summary.

/// Creates a new string whose characters are the result of applying
/// the function mapping to each of the characters of the input string
/// and concatenating the resulting strings.
val collect : (char -> string) -> string -> string

If a comment body begins with < (normally <summary>), then it is treated as an XML formatted comment body using XML tags. This second way enables you to specify separate notes for a short summary, additional remarks, documentation for each parameter and type parameter and exceptions thrown, and a description of the return value.

The following is a typical XML documentation comment in a signature file:

/// <summary>Builds a new string whose characters are the results of applying the function <c>mapping</c>
/// to each of the characters of the input string and concatenating the resulting
/// strings.</summary>
/// <param name="mapping">The function to produce a string from each character of the input string.</param>
///<param name="str">The input string.</param>
///<returns>The concatenated string.</returns>
///<exception cref="System.ArgumentNullException">Thrown when the input string is null.</exception>
val collect : (char -> string) -> string -> string

If you are using XML tags, the following table describes the outer tags recognized in F# XML code comments.

Tag syntax Description
text Specifies that text is a brief description of the program element. The description is usually one or two sentences.
text Specifies that text contains supplementary information about the program element.
description Specifies the name and description for a function or method parameter.
description Specifies the name and description for a type parameter.
text Specifies that text describes the return value of a function or method.
description Specifies the type of exception that can be generated and the circumstances under which it is thrown.
Specifies a See Also link to the documentation for another type. The reference is the name as it appears in the XML documentation file. See Also links usually appear at the bottom of a documentation page.

The following table describes the tags for use inside description sections:

Tag syntax Description
text Specifies a paragraph of text. This is used to separate text inside the remarks tag.
text Specifies that text is multiple lines of code. This tag can be used by documentation generators to display text in a font that is appropriate for code.
Specifies a reference to a parameter in the same documentation comment.
Specifies a reference to a type parameter in the same documentation comment.
text Specifies that text is inline code. This tag can be used by documentation generators to display text in a font that is appropriate for code.
text Specifies an inline link to another program element. The reference is the name as it appears in the XML documentation file. The text is the text shown in the link.

User-defined tags

The previous tags represent those that are recognized by the F# compiler and typical F# editor tooling. However, a user is free to define their own tags. Tools like fsdocs bring support for extra tags like . Custom or in-house documentation generation tools can also be used with the standard tags and multiple output formats from HTML to PDF can be supported.

Compile-time checking

When --warnon:3390 is enabled, the compiler verifies the syntax of the XML and the parameters referred to in <param> and <paramref> tags.

Documenting F# Constructs

F# constructs such as modules, members, union cases, and record fields are documented by a /// comment immediately prior to their declaration. If needed, implicit constructors of classes are documented by giving a /// comment prior to the argument list. For example:

/// This is the type
type SomeType
      /// This is the implicit constructor
      (a: int, b: int) =

    /// This is the member
    member _.Sum() = a + b

Limitations

Some features of XML documentation in C# and other .NET languages are not supported in F#.

Recommendations

Documenting code is recommended for many reasons. What follows are some best practices, general use case scenarios, and things that you should know when using XML documentation tags in your F# code.

See also