F#: Enforce the creation of a Discriminated Union value via a dedicated function

I posted this question.

The link above reflects a question that I had regarding how to enforce reliance on a function to create a value for a client.

The solution is to have the client rely on Active Patterns that’s publicly exposed while maintaining the privacy of the Discriminated Union type. This will prevent a client from creating a value of it explicitly.

Here’s the solution:

namespace Mynamespace

module File1 =

    type EmailAddress = 
        | Valid   of string 
        | Invalid of string

    let createEmailAddress (address:string) =
        if   address.Length > 0
        then Valid   address 
        else Invalid address

    let (|Valid|Invalid|) = function
        | Valid   input -> Valid   input
        | Invalid input -> Invalid input

module File2 =

    let result = createEmailAddress "XXX" |> function
                 | Valid    email -> true
                 | Invalid  email -> false

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s