Enum (flags) member composed of other members

2020-03-08 12:37发布

[<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#?

标签: f#
2条回答
不美不萌又怎样
2楼-- · 2020-03-08 13:24

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
查看更多
【Aperson】
3楼-- · 2020-03-08 13:35

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

Grammar

type enum-name =
   | value1 = integer-literal1
   | value2 = integer-literal2
查看更多
登录 后发表回答