I am trying to define a generic conversion operator from a string to Enum, and I would like to use it as follows:
let day = asEnum<DayOfWeek>("Monday")
But with this implementation:
let asEnum<'a, 'b when 'a: (new : unit -> 'a) and 'a : struct and 'a :> ValueType and 'a : enum<'b>> text = match Enum.TryParse<'a>(text) with | true, value -> Some value | false, _ -> None
I can only use it like this:
let day = asEnum<DayOfWeek,_>("Monday")
or that:
let day:DayOfWeek option = asEnum("Monday")
If I omitted 'a : enum<'b> from the type constraint at all, I can get it the way I want, but if someone does not specify the type, the default will be int , which I really dislike, I would prefer he gave a compile-time error, how does this happen when I specify a limit
Maybe there is some trick to just specify one type parameter and draw a different conclusion? Any ideas?
Gustavo guerra
source share