让我们考虑一下C#中的以下枚举
public enum ScrollMode : byte
{
None = 0,
Left = 1,
Right = 2,
Up = 3,
Down = 4
}
Run Code Online (Sandbox Code Playgroud)
F#代码接收一个字节,并且必须返回我尝试过的枚举实例
let mode = 1uy
let x = (ScrollMode)mode
Run Code Online (Sandbox Code Playgroud)
(当然在实际应用中我没有设置'模式',它是作为网络数据的一部分接收的).
上面的例子没有编译,有什么建议吗?
Bri*_*ian 18
对于底层类型为'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
Run Code Online (Sandbox Code Playgroud)
(此处列出了EnumOfValue
(现在http://msdn.microsoft.com/en-us/library/ee340276(VS.100).aspx)
而枚举列在这里
(现在http://msdn.microsoft.com/en-us/library/ee353754(VS.100).aspx))