I want to convert a string representations of few dozen enum types to enum values.
It’s easy to convert string to concrete type:
Enum.Parse(typeof<FontStyle>,"Bold") |> unbox<FontStyle>
but for now i want to write function where type and string are parameters.
The best one i can write is:
> let s2e (_: 'a) s = Enum.Parse(typeof<'a>,s) |> unbox<'a>;;
val s2e : 'a -> string -> 'a
> s2e FontStyle.Regular "Bold";;
val it : FontStyle = Bold
Is there any option to write something like this but with type itself as first argument?
The function needs to take a single type parameter, which will be the type of the returned enum. However, you don’t need to specify the type using a “fake” parameter that isn’t used anywhere in the code – you can specify the actual type parameter when calling the function using the same notation as for example when calling
defaultof<SomeType>.The following modified function takes a single type parameter which occurs only in the return type (to avoid confusing SO code formatter, I replaced ‘ with ´ in the code):
When calling the function, you need to specify the type explicitly:
Your solution was pretty close to this – the only change is that you can specify the type parameter explicitly instead of providing a witness value to guide the type inference.