Thomas
Thomas

Reputation: 12107

Not understanding this F# array issue

I want to enumerate an enum:

type T =
| Type1 = 0
| Type2 = 1

let a = Enum.GetValues(typeof<T>)

which gives me this:

val a : Array = [|T1; T2|]

now, I want to go through the array:

a |> Array.iter (fun x -> ())

but I get that:

Type mismatch. Expecting a 'Array -> 'a'
but given a ''b [] -> unit'
The type 'Array' does not match the type ''a []'

I don't understand why I have an array that doesn't match the array type...

Upvotes: 1

Views: 154

Answers (1)

Lee
Lee

Reputation: 144136

Enum.GetValues returns a System.Array but Array.iter requires a generic array type. You can cast the value returned from GetValues:

let a = Enum.GetValues(typeof<T>) :?> T array
a |> Array.iter (fun x -> ())

Upvotes: 2

Related Questions