gpt4 book ai didi

types - 为什么这个 Discriminated Union 不接受 Set 类型的案例?

转载 作者:行者123 更新时间:2023-12-01 23:16:11 27 4
gpt4 key购买 nike

我正在尝试创建一个新的 Set 类型:

type MySet<'t> = | List of list<'t>
| Sequence of seq<'t>
| Array of 't []

这行得通,但是如果我尝试为 Set 类型本身添加一个 case,我会收到一条消息:a type parameter is missing a constraint 'when t: comparison'

type MySet<'t> = | List of list<'t>
| Sequence of seq<'t>
| Array of 't []
| Set of Set<'T>

我的猜测是这应该很容易修复,但即使我尝试了一些方法我也做不到。

最佳答案

执行Set<'t>数据结构要求其值可以比较,因此如果您的类型包含可以放入集合中的值,则必须提供相同的类型约束:

type MySet<'t when 't : comparison> =
| List of list<'t>
| Sequence of seq<'t>
| Array of 't []
| Set of Set<'t>

关于types - 为什么这个 Discriminated Union 不接受 Set 类型的案例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50178065/

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