Enum (flags) member composed of other members
[<Flags>]
type LikeMatch =
| None = 0
| Start = 1
| End = 2
| All = Start ||| End //ERROR: U开发者_如何学Gonexpected identifier in union case
I've also tried qualifying the members with the enum type. Is there a way to do this in F#?
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
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
We can define flag combinations:
- Inline - see Robert's answer
- In a companion module
Each option has its pros and cons.
1. Inline combinations by hand
[<System.Flags>]
type LikeMatch =
| None = 0b00
| Start = 0b01
| End = 0b10
| All = 0b11 // ❌ Not human-friendly (more difficult to understand it's "Start + End")
let all = LikeMatch.All
// val all: LikeMatch = All
let all' = LikeMatch.Start ||| LikeMatch.End
// val all': LikeMatch = All
// ✔️ Compiler-friendly
2. In a companion module
[<System.Flags>]
type Spacing =
| Left = 0b0001
| Right = 0b0010
| Top = 0b0100
| Bottom = 0b1000
[<RequireQualifiedAccess>]
module Spacing =
let Horizontal = Spacing.Left ||| Spacing.Right // ✔️ Human-friendly
let Vertical = Spacing.Top ||| Spacing.Bottom
let All = Horizontal ||| Vertical
let horizontal = Spacing.Horizontal
// val horizontal: Spacing = Left, Right
// ❌ Not "Horizontal"
There's also a difference regarding HasFlag
, whether combinations are included or not:
[<RequireQualifiedAccess>]
module Enum =
let values<'enum when 'enum :> System.Enum> =
System.Enum.GetValues(typeof<'enum>)
:?> 'enum array
|> Array.toList
let flags<'enum when 'enum :> System.Enum> (enumValue: 'enum) =
values<'enum>
|> List.filter (enumValue.HasFlag)
let flagsInline = Enum.flags LikeMatch.All
// val flagsInline: LikeMatch list = [None; Start; End; All]
// ⚠️ Includes "None" and "All"
let flagsCompanion = Enum.flags Spacing.All
// val flagsCompanion: Spacing list = [Left; Right; Top; Bottom]
//
精彩评论