Created
July 3, 2022 14:51
-
-
Save otto-gebb/f4d4d2cea5fd688d8f3a26a90ebbbc00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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