gpt4 book ai didi

types - 与文字的可区分联合

转载 作者:行者123 更新时间:2023-12-01 08:30:46 26 4
gpt4 key购买 nike

对于位类型,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.

最佳答案

如果您正在寻找 Bit 的类型安全表示,您可能更喜欢 DU 而不是枚举以进行详尽的模式匹配:

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 作为内部表示。

关于types - 与文字的可区分联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com