In this post, we look at a key benefit of F#, which using the type system to “make illegal states unrepresentable” (a phrase borrowed from Yaron Minsky).
Now let’s say that we have the following simple business rule: “A contact must have an email or a postal address”. Does our type conform to this rule?
The answer is no. The business rule implies that a contact might have an email address but no postal address, or vice versa. But as it stands, our type requires that a contact must always have both pieces of information.
But now we have gone too far the other way. In this design, it would be possible for a contact to have neither type of address at all. But the business rule says that at least one piece of information must be present.
This design meets the requirements perfectly. All three cases are explicitly represented, and the fourth possible case (with no email or postal address at all) is not allowed.