Question

I want to create something that's kind of like an enum with an F# record type for a value instead of an int. For example, if I've got the union:

type BologneseIngredients = | Spaghetti
                            | Tomatoes
                            | MincedBeef
                            | GrandmasSecretIngredient

I know that spaghetti is always 30cm long and tomatoes are always red. What I could do is have a 'get metadata' function:

let getMetadata = function
                    | Spaghetti -> { length: 30.0<cm> }
                    | Tomatoes -> { colour: Color.Red }
                    | _ -> { }

but I'd really like to keep the definition of the union and the data together. Is there a nice way to do this?

Was it helpful?

Solution

my suggestion:

module Recipes =

    type BologneseIngredients = | Spaghetti
                                | Tomatoes
                                | MincedBeef
                                | GrandmasSecretIngredient

    let length (ind : BologneseIngredients) : float<cm> option =
         match ind with
         | Sphaghetti -> Some 30.0<cm>
         | _ -> None

    // .. or a bit more "metadata"ish
    type Metadata = 
        | Length of float<cm>
        | Color of System.Drawing.Color

    let metadata = 
       function
       | Sphaghetti -> [ Length 30.0<cm ]
       | Tomatoes   -> [ Color System.Drawing.Color.Red ]
       | ...

    let metaLength meta =
       meta |> List.tryPick (function | Length l -> Some l | _ -> None)

    let getLength = metadata >> metaLength

OTHER TIPS

You could add properties to your discriminated union...

type BologneseIngredients = 
    | Spaghetti
    | Tomatoes
    | MincedBeef
    | GrandmasSecretIngredient

    member x.Color =
        match x with
        | Spaghetti -> Color.AntiqueWhite
        | Tomatoes -> Color.Red
        | MincedBeef -> Color.Firebrick
        | GrandmasSecretIngredient -> Color.Transparent



let foo = Tomatoes

printfn "%A" foo.Color

> Color [Red]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top