If I have the following enum in F#:
type Note =
| C = 0
| D = 2
| E = 4
| F = 5
| G = 7
| A = 9
| B = 11
But at some points, I need the ordinals of the items inside of it, such that Note.C = 1, Note.D = 2,
etc. Obviously in other languages, foo.ordinal()
would work, but in here is it necessary to use a function like such:
let getNoteOrdinal = function
|Note.C -> 1
|Note.D -> 2
|Note.E -> 3
|Note.F -> 4
//etc
Is that function necessary or is there something better?
Enums in .NET are just a layer on top of an integral type, in this case your values are int
.
Per the docs, you can do a simple conversion to get that value, for example:
let value = int Note.B
Where value
in this case would be 11
.
To find the ordinal, as you can make use of a built-in function that gives you an array of all the values in order and then find the index of the one you're after. For example:
let getNoteOrdinal (note: Note) =
let values = Enum.GetValues(typeof<Note>)
Array.IndexOf(values, note) + 1
let value = getNoteOrdinal Note.E
Where value
in this case would be 3
.
You can take this one step further as @ReedCopsey suggests and make a generic function that will give you the ordinal for any enum value:
let getEnumOrdinal (e: 'a when 'a : enum<'b> and 'a : equality) =
let values = Enum.GetValues(e.GetType())
Array.IndexOf(values, e) + 1
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments