Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documenting discriminated unions in F#

Tags:

f#

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?

like image 807
Steven Avatar asked Sep 28 '15 15:09

Steven


People also ask

How is a discriminated union defined?

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.

What is a tagged union C++?

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).

Does C# have union types?

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.


2 Answers

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.

like image 114
Tomas Petricek Avatar answered Nov 15 '22 10:11

Tomas Petricek


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.

like image 39
piaste Avatar answered Nov 15 '22 12:11

piaste