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

在f中将字节转换为枚举的实例#

  •  10
  • TimothyP  · 技术社区  · 16 年前

    让我们考虑一下C中的以下枚举#

    public enum ScrollMode : byte
    {
          None = 0,
          Left = 1,
          Right = 2,
          Up = 3,
          Down = 4
    }
    

    F代码接收一个字节,必须返回枚举的实例 我试过了

    let mode = 1uy
    let x = (ScrollMode)mode
    

    (当然,在实际应用中,我没有设置“模式”, 它作为网络数据的一部分接收)。

    上面的例子没有编译,有什么建议吗?

    2 回复  |  直到 13 年前
        1
  •  17
  •   Brian    15 年前

    对于基础类型为“int”的枚举,“enum”函数将执行转换,但对于非int枚举,则需要“LanguagePrimitives.EnumOfValue”la:

    // define an enumerated type with an sbyte implementation
    type EnumType =
      | Zero = 0y
      | Ten  = 10y
    
    // examples to convert to and from
    let x = sbyte EnumType.Zero
    let y : EnumType = LanguagePrimitives.EnumOfValue 10y
    

    (此处列出EnumOfValue

    http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/FSharp.Core/Microsoft.FSharp.Core.LanguagePrimitives.html

    (现在) http://msdn.microsoft.com/en-us/library/ee340276(VS.100).aspx )

    鉴于枚举列在此处

    http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/FSharp.Core/Microsoft.FSharp.Core.Operators.html

    (现在) http://msdn.microsoft.com/en-us/library/ee353754(VS.100).aspx ) )

        2
  •  2
  •   user202863    15 年前

    设x:scrollmode=enum模式