位类型在F#中的正确语法是什么,只有两个可能的值:0和1。我试过了;
type bit =
| 0
| 1错误消息是error FS0010: Unexpected integer literal in union case。我需要使用[<Literal>]吗?
我收到了一个不同的错误消息:error FS0010: Unexpected integer literal in union case. Expected identifier, '(', '(*)' or other token.
发布于 2012-11-03 15:21:07
如果您正在寻找Bit的类型安全表示,您可能更喜欢使用DUs而不是枚举来进行详尽的模式匹配:
type Bit = Zero | One
with member x.Value =
match x with
| Zero -> 0
| One -> 1如果你想要一个紧凑的表示,boolean类型是一个很好的选择:
let [<Literal>] Zero = false
let [<Literal>] One = true
let check = function
| Zero -> "It's Zero"
| One -> "It's One"当有一个Bit集合时,您可以查看BitArray以获得更有效的处理。他们确实使用boolean作为内部表示。
发布于 2012-11-03 10:13:18
你想使用枚举-
type bit =
| Zero= 0
| One = 1尽管模式匹配不如区分联合那么好。
或者,您也可以将DU与
type bit =
|Zero
|One
member x.Int() =
match x with
|Zero -> 0
|One -> 1https://stackoverflow.com/questions/13205177
复制相似问题