【发布时间】:2017-06-08 00:04:06
【问题描述】:
如果我有一个有多个值共享一个孩子的可区分联合(Apple 和 MoreApples 都具有Apple 类型)...
type Apples =
| GrannySmith
| Gala
type Fruit =
| Apple of Apples
| MoreApples of Apples
| Banana
let speakFruit = function
| Apple GrannySmith
| MoreApples GrannySmith -> "granny smith"
| Apple Gala
| MoreApples Gala -> "gala"
| Banana -> "banana"
有没有办法在子联合上进行匹配以消除重复? - 类似:
let speakFruit2 = function
| _ GrannySmith -> "granny smith"
| _ Gala -> "gala"
| Banana -> "banana"
【问题讨论】:
-
您是完全控制您的类型还是他们是第 3 方?如果是前者,那么可能会做类似this 的事情吗?
-
没有办法做到这一点,这实际上表明您的类型不能很好地反映您的域。