Is there a "best practices" for documenting Discriminated Unions in F#? I've been using the XML
tags available at the MSDN website, but there's no mention on documenting DUs, other than the <typeparam name = "x"> Desc. </typeparam>
tags.
The tags are helpful for standard types and functions, but which XML
tags should be used for DUs?
Discriminated unions are useful for heterogeneous data; data that can have special cases, including valid and error cases; data that varies in type from one instance to another; and as an alternative for small object hierarchies. In addition, recursive discriminated unions are used to represent tree data structures.
Simply put, tagged unions are unions that have associated with them a piece of data that tracks which of the potential union properties is currently set. In C++ you can choose between structs and classes to encapsulate the union, or develop your own custom data structure base solution (like a map).
Union types are common in functional languages, but have yet to make it to C# and similar. These types solve a simple and very common problem, data that is one of a finite set of possibilities.
I mostly just use the <summary>
tag for the type and for all its members (and since the compiler adds <summary>
automatically, this means I don't have to write any XML by hand):
/// Represents a thumbnail that will appear on the movie web site
type MovieThumbnail =
/// Use a PNG image at the specified URL
| Image of string
/// Use a default image for the specified genre
| Default of Genre
It might be just me, but I find that filing all the other tags is just too much work and it does not give you that much more information.
If you were using F# ProjectScaffold, then the documentation tool also supports Markdown in the XML comments, and so you could write for example:
/// Represents a thumbnail that will appear on the movie web site
///
/// ## Example
/// The following shows simple pattern matching:
///
/// match movieThumb with
/// | Image(url) -> sprintf "<img src='%s' />" url
/// | Default(g) -> defaultImageFor g
///
type MovieThumbnail =
/// Use a PNG image at the specified URL
| Image of string
/// Use a default image for the specified genre
| Default of Genre
At the moment, this does not show very nicely in Visual Studio tooltips, but if you are writing a library and want to have a great documentation for it, then this is a good way to get it.
Each union member is, in effect, its own type, and it can have its own XML comment documentation. So you can write a DU like this:
/// Explain Foo here
type Foo =
/// Explain Bar here
| Bar
/// Explain Baz here
| Baz
and you'll get each comment in the tooltip when hovering over the appropriate type name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With