F#选择类型的用例 [英] Use case for F# Choice type

查看:69
本文介绍了F#选择类型的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我已经知道 F#Choice 类型,但是可以不会想到我会在任何地方使用它,而不是使用有意义的名称定义自己的联盟类型案例.

MSDN文档没有提供太多建议(具有两种选择的活动模式的帮助程序类型".)并且没有任何示例用法.

我必须缺少一些东西-与自定义联合相比,这种类型的主要优点是什么?

在我看来,Choice类型的用例与元组类型的用例非常相似.在这两种情况下,您通常都希望定义自己的更具体的类型,该类型与该类型同构(用于选择的自定义DU;用于元组的自定义记录类型).尽管如此,在有限的范围内或在非常普通的情况下(好的命名可能会变得困难),使用匿名变体还是很不错的.

I've been aware of the F# Choice type for a while , but can't think of any place I'd use it rather than defining my own union type with meaningful named cases.

The MSDN documentation doesn't offer much advice ("Helper types for active patterns with two choices.") and doesn't have any example usages.

I must be missing something - what are the key advantages of this type over a custom union?

解决方案

In my opinion the use cases for the Choice type are quite similar to the use cases for tuple types. In either case, you will often want to define your own more specific type which is isomorphic to the type (a custom DU for choices; a custom record type for tuples). Nonetheless, over limited scopes or in very generic situations (where good naming may become difficult), it's nice to have anonymous variants.

这篇关于F#选择类型的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆