We can specify an underlying type for enum in C# like this:
[Flags]
public enum MyKinds : ushort
{
None = 0,
Flag1 = 1 << 0,
Flag2 = 1 << 1,
// ...
}
- How can I do that using F# ?
type MyKinds =
| None = 0
| Flag1 = 1
| Flag2 = 2
// inherit ushort // error FS0912
- How can I define enum values using bitwise operator like
1 << 0in F# ?
type MyKinds =
| None = 0
| Flag1 = 1 << 0 // error FS0010
| Flag2 = 1 << 1 // error FS0010
>Solution :
As specified in The Docs, the underlying type of an enum in F# is determined by the numerical suffix.
So in your case, for ushort (unit16), it would be:
type MyKinds =
| None = 0us
| Flag1 = 1us
| Flag2 = 2us
(suffixes for different number types available Here)