代码之家  ›  专栏  ›  技术社区  ›  Jared

如何枚举f中的枚举/类型#

  •  12
  • Jared  · 技术社区  · 15 年前

    我有一个枚举类型定义如下:

    type tags = 
        | ART  = 0
        | N    = 1
        | V    = 2 
        | P    = 3
        | NULL = 4
    

    有办法吗 for ... in tags do ?

    这是我得到的错误:

    值、构造函数、命名空间或 类型 tags 未定义

    7 回复  |  直到 6 年前
        1
  •  8
  •   jason    15 年前

    使用 Enum.GetValues :

    let allTags = Enum.GetValues(typeof<tags>)
    
        2
  •  6
  •   Tomas Petricek    15 年前

    这里有一个完整的例子,打印关于任何歧视工会的信息。它演示了如何获取歧视工会的案例,以及如何获取字段(如果需要)。函数打印给定的区分联合的类型声明:

    open System
    open Microsoft.FSharp.Reflection
    
    let printUnionInfo (typ:Type) = 
      printfn "type %s =" typ.Name
      // For all discriminated union cases
      for case in FSharpType.GetUnionCases(typ) do
        printf "  | %s" case.Name
        let flds = case.GetFields()
        // If there are any fields, print field infos
        if flds.Length > 0 then 
          // Concatenate names of types of the fields
          let args = String.concat " * " [ for fld in flds -> fld.PropertyType.Name ] 
          printf " of %s" args
        printfn ""    
    
    // Example
    printUnionInfo(typeof<option<int>>)
    
        3
  •  3
  •   Kristopher Johnson    10 年前

    你可以使用 Enum.GetValues ,返回 Array 然后必须向下转换为整数值的对象。(注意:我正在使用Mono的f实现;也许.NET的情况不同。)

    以下是我编写的一些函数,用于获取所有枚举值的列表,以及获取最小值和最大值:

    open System
    
    module EnumUtil =
    
        /// Return all values for an enumeration type
        let EnumValues (enumType : Type) : int list =
            let values = Enum.GetValues enumType
            let lb = values.GetLowerBound 0
            let ub = values.GetUpperBound 0
            [lb .. ub] |> List.map (fun i -> values.GetValue i :?> int) 
    
        /// Return minimum and maximum values for an enumeration type
        let EnumValueRange (enumType : Type) : int * int =
            let values = EnumValues enumType
            (List.min values), (List.max values)
    
        4
  •  2
  •   Robert    15 年前

    要使其成为枚举,需要显式地为每种情况赋予值,否则它是联合类型:

    type tags = 
        | ART = 0
        | N = 1
        | V = 2
        | P = 3
        | NULL= 4
    let allTags = System.Enum.GetValues(typeof<tags>)
    
        5
  •  2
  •   kvb    15 年前

    罗伯特关于如何生成实际枚举并获取其事例的正确性。如果您有一个真正的联合类型,您可以通过 Microsoft.FSharp.Reflection.FSharpType.GetUnionCases 功能。

        6
  •  2
  •   bill    6 年前

    怎么样:

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

    这具有提供强类型列表的优势

    只需使用:

    let tagList = enumToList<tags>
    
        7
  •  0
  •   ympostor Juliet    7 年前
    type Options = 
        | Exit          = 0
        | CreateAccount = 1
    
    Console.WriteLine()
    Console.WriteLine("Choose an option:")
    let allOptions = Enum.GetValues(typeof<Options>)
    for option in allOptions do
        if (option <> null) then
            Console.WriteLine(sprintf "%d: %s" (option :?> int) (option.ToString()))
    let optionChosen = System.Console.ReadLine()