Skip to content

Instantly share code, notes, and snippets.

@otto-gebb
Created July 3, 2022 14:51
Show Gist options
  • Save otto-gebb/f4d4d2cea5fd688d8f3a26a90ebbbc00 to your computer and use it in GitHub Desktop.
Save otto-gebb/f4d4d2cea5fd688d8f3a26a90ebbbc00 to your computer and use it in GitHub Desktop.
let inline fromStr< ^T when ^T : enum<int32> and ^T : equality >(s: string) : Option< ^T > =
let t = typeof<'T>
let enumStrings = System.Enum.GetNames(t)
let vals = System.Enum.GetValues(t) :?> 'T[]
Array.zip vals enumStrings
|> Array.tryFind (fun (x, s') -> s' = s)
|> Option.map fst
let inline toStr< ^T when ^T : enum<int32> and ^T : equality >(x: ^T) =
let t = typeof<'T>
let enumStrings = System.Enum.GetNames(t)
let vals = System.Enum.GetValues(t) :?> 'T[]
Array.zip vals enumStrings
|> Array.find (fun (x', s) -> x' = x)
|> snd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment