1 people like it.

Creating a list of all the elements of an Enum

I have seen a few postings on the web about this, with not terribly elegant solutions. Here is how I do it.

1: 
let enumToList<'a> = (System.Enum.GetValues(typeof<'a>) :?> ('a [])) |> Array.toList
val enumToList<'a> : 'a list

Full name: Script.enumToList
namespace System
type Enum =
  member CompareTo : target:obj -> int
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int
  member GetTypeCode : unit -> TypeCode
  member HasFlag : flag:Enum -> bool
  member ToString : unit -> string + 3 overloads
  static member Format : enumType:Type * value:obj * format:string -> string
  static member GetName : enumType:Type * value:obj -> string
  static member GetNames : enumType:Type -> string[]
  static member GetUnderlyingType : enumType:Type -> Type
  ...

Full name: System.Enum
System.Enum.GetValues(enumType: System.Type) : System.Array
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
module Array

from Microsoft.FSharp.Collections
val toList : array:'T [] -> 'T list

Full name: Microsoft.FSharp.Collections.Array.toList
Raw view New version

More information

Link:http://fssnip.net/7UO
Posted:8 days ago
Author:billhay
Tags: #enum #list