首页 > 解决方案 > 为什么这个 Discriminate Union 不接受 Set 类型的情况?

问题描述

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

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

这可行,但如果我尝试为Set类型本身添加案例,我会收到一条消息: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>

我的猜测是这应该很容易修复,但即使我尝试了几件事,我也无法做到。

标签: typesf#setdiscriminated-union

解决方案


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

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

推荐阅读