Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract value from union case

Tags:

f#

In an Fsharp application, I have defined several union case types as

type A = A of String
type B = B of String
type C = C of String

I would like to define a function to extract the value from the union case instances.

let getValue( ctor: String-> 'a) = 
   ...implementation here

Is there anyway to achieve such task? Thanks.

like image 831
Wei Ma Avatar asked Jun 14 '14 06:06

Wei Ma


1 Answers

Let's say you have:

type A = A of string
type B = B of string
type C = C of string

let a = A "hello"
let b = B "world"
let c = C "!"

There are many ways to extract those values, here are some:

Individual unwrappers

let getValueA (A v) = v
let getValueB (B v) = v
let getValueC (C v) = v

let valueOfA = getValueA a
let valueOfB = getValueB b
let valueOfC = getValueC c

Method overload

type T =
    static member getValue (A v) = v
    static member getValue (B v) = v
    static member getValue (C v) = v

let valueOfA = T.getValue a
let valueOfB = T.getValue b
let valueOfC = T.getValue c

Function overload

type GetValue = GetValue with
    static member ($) (GetValue, (A v)) = v
    static member ($) (GetValue, (B v)) = v
    static member ($) (GetValue, (C v)) = v

let inline getValue x : string = GetValue $ x

let valueOfA = getValue a
let valueOfB = getValue b
let valueOfC = getValue c

Reflection

open Microsoft.FSharp.Reflection
let getValue a =  
    FSharpValue.GetUnionFields (a, a.GetType())
        |> snd
        |> Seq.head
        :?> string

let valueOfA = getValue a
let valueOfB = getValue b
let valueOfC = getValue c

Redesign your DU

type A = A
type B = B
type C = C

type MyDU<'a> = MyDU of 'a * string

let a = MyDU (A, "hello")
let b = MyDU (B, "world")
let c = MyDU (C, "!"    )

let getValue (MyDU (_, v)) = v

let valueOfA = getValue a
let valueOfB = getValue b

Redesign with interfaces

type IWrapped<'a> =
    abstract getValue: 'a

type A = A of string with interface IWrapped<string> with member t.getValue = let (A x) = t in x        
type B = B of string with interface IWrapped<string> with member t.getValue = let (B x) = t in x
type C = C of string with interface IWrapped<string> with member t.getValue = let (C x) = t in x

let valueOfA = (a :> IWrapped<string>).getValue
like image 106
Gus Avatar answered Nov 20 '22 19:11

Gus