Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to specify an underlying type for enum in F#

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,
    // ...
}
  1. How can I do that using F# ?
type MyKinds = 
    | None  = 0 
    | Flag1 = 1
    | Flag2 = 2

    // inherit ushort  // error FS0912
  1. How can I define enum values using bitwise operator like 1 << 0 in F# ?
type MyKinds = 
    | None = 0 
    | Flag1 = 1 << 0    // error FS0010
    | Flag2 = 1 << 1    // error FS0010

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading