变体或多态变体? [英] Variants or Polymorphic variants?

查看:148
本文介绍了变体或多态变体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在我知道的OCaml程序员中,其中一些总是使用多态变体(未声明的变体,以反引号作为前缀),而其他 使用多态变体,并且喜欢在类型中声明的变体。



除了性能原因(多态变体目前编译效率低于简单变体),专家OCaml开发人员如何选择在他们之间?

解决方案

我的用法可以分为以下5个类别。 1.界面2.模块化3.易读性4.简洁性5.技巧


  1. 如果变体类型仅在模块内部,常规变体,因为正如你所说,它们的编译效率更高。

  2. 如果在接口中导出变体类型,并且我觉得有些情况可能出现在其他模块中,但是使它们依赖于模块并不一定有意义,我使用多态因为它们没有绑定到模块名称空间系统。示例: Xmlm 类型的编码类型类型。 code>。将信号类型作为变体类型也意味着您可以使用在不引入对 Xmlm 的依赖关系的情况下进行XML处理的相同想法。
  3. 如果在接口中导出变体类型,我有时也会发现它当变体类型的值被赋予模块的功能时,详细地使用常规变体。例如: Uuidm 版本类型 C $ C>。而不必编写 Uuidm.create Uuidm.V4 ,你可以简单地写 Uuidm.create`V4 ,这就是清晰且不太冗长。
  4. 有时某个特定的函数可能会返回不同的情况。如果这些情况只被这个函数使用,我在界面中声明函数类型,而不必引入类型定义。例如 parse:string - > [`字符串错误| `好的

  5. 多态变体及其子类型允许您使用幻像类型静态执行不变量。除了以增量方式定义它们的可能性之外,这对于静态地执行不变量以及用于文档目的都是有用的。

最后,我有时在实现根据4的模块时使用多态变体,但是没有它们出现在界面中。我不鼓励这种用法,除非你声明多态变体并关闭它们,因为它削弱了静态输入规则。

I noticed that, among OCaml programmers I know, some of them always use polymorphic variants (variants that are not declared, prefixed with a backquote), while other ones never use polymorphic variants, and prefer variants declared in types.

Except for performance reasons (polymorphic variants are currently compiled less efficiently than simple variants), how do expert OCaml developers choose between them ?

解决方案

My usage can be divided into the following 5 categories. 1. interface 2. modularity 3. legibility 4. brevity 5. tricks

  1. If the variant type is only internal to the module, I use regular variants, because as you said they are compiled more efficiently.
  2. If the variant type is exported in the interface and I feel that some cases could appear in other modules but it wouldn't necessarily make sense to make them dependend on the module, I use polymorphic variants because they are not tied to the module namespace system. Examples: the encoding type type of Xmlm. Also having the signal type as a variant type means you can develop modules using the same idea for XML processing without introducing a dependency on Xmlm.
  3. If the variant type is exported in the interface I find it sometimes too verbose to use regular variants when values of the variant type are given to functions of the module. Example: the version type of Uuidm. Instead of having to write Uuidm.create Uuidm.V4 you can simply write Uuidm.create `V4, which is as clear and less verbose.
  4. Sometimes a particular function may return different cases. If these cases are only used by this function I declare the function type in the interface without having to introduce a type definition. For example parse : string -> [`Error of string | `Ok of t]
  5. Polymorphic variants and their subtyping allow you enforce invariants statically with phantom types. Besides the possibility of defining them incrementally can be useful, both for enforcing invariants statically and for documentation purposes.

Finally I sometimes use polymorphic variants in the implementation of a module according to 4. but without them showing up in the interface. I discourage this usage unless you declare the polymorphic variants and close them because it weakens the static typing discipline.

这篇关于变体或多态变体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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