Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think that using type aliases for documentation purposes is a good and simple way to document your discriminated unions. I use the same approach in many of my demos (see <a href="https://github.com/tpetricek/Documents/blob/master/Talks%202011/F%23%20Tutorial%20(QCon%20London)/Checkout/Domain.fs" rel="noreferrer">for example this one</a>) and I know that some people use it in production applications too. I think there are two ways to make the definition more self-explanatory:</p> <p><strong>Use type aliases:</strong> This way, you add some documentation that is visible in the IntelliSense, but it doesn't propagate through the type system - when you used a value of the aliased type, the compiler will treat it as <code>string</code>, so you don't see the additional documentation everywhere.</p> <p><strong>Use single-case unions</strong> This is a pattern that has been used in some places of the F# compiler. It makes the information more visible than using type-aliases, because a type <code>SenderName</code> is actually a different type than <code>string</code> (on the other hand, this may have some small performance penalty):</p> <pre><code>type SenderName = SenderName of string type RecipientName = RecipientName of string type NetworkElement = | Message of SenderName * RecipietName * string match netelem with | Message(SenderName sender, RecipientName recipiet, msg) -&gt; ... </code></pre> <p><strong>Use records:</strong> This way, you explicitly define a record to carry the information of a union case. This is more syntactically verbose, but it probably adds the additional information in the most accessible way. You can still use pattern matching on records, or you can use dot notation to access elements. It is also easier to add new fields during the development:</p> <pre><code>type MessageData = { SenderName : string; RecipientName : string; Message : string } type NetworkEvent = | Message of MessageData match netelem with | Message{ SenderName = sender; RecipientName = recipiet; Message = msg} -&gt; ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload