我正在尝试创建一个新的 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/