Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum (flags) member composed of other members

Tags:

f#

[<Flags>]
type LikeMatch =
    | None  = 0
    | Start = 1
    | End   = 2
    | All   = Start ||| End //ERROR: Unexpected identifier in union case

I've also tried qualifying the members with the enum type. Is there a way to do this in F#?

like image 998
Daniel Avatar asked May 19 '10 15:05

Daniel


People also ask

What is flag enum?

The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value. Such enumeration collections are usually manipulated using bitwise operators.

What is the role of flag attribute in enum?

The [Flag] attribute is used when Enum represents a collection of multiple possible values rather than a single value. All the possible combination of values will come. The [Flags] attribute should be used whenever the enumerable represents a collection of possible values, rather than a single value.

How do you flag an enum?

The "|=" operator actually adds a flag to the enum, so the enum now contains two flag bits. You can use "|=" to add bits, while & will test bits without setting them. And Bitwise AND returns a value with 1 in the targeted bit if both values contain the bit.


2 Answers

As JaredPar says it's not allowed by the language, but F# does have binary literals which makes it easy to show which bits are being set:

open System

[<Flags>]
type LikeMatch =
    | None  = 0b000000000
    | Start = 0b000000001
    | End   = 0b000000010
    | All   = 0b000000011
like image 102
Robert Avatar answered Dec 01 '22 00:12

Robert


According to the F# language reference there is no way to do this. The right hand side of the = sign in a F# enum must be an integer literal

  • http://msdn.microsoft.com/en-us/library/dd233216(v=VS.100).aspx

Grammar

type enum-name =
   | value1 = integer-literal1
   | value2 = integer-literal2
like image 25
JaredPar Avatar answered Dec 01 '22 00:12

JaredPar