对于F#中的DU,是否存在OCaml等效的[<requirequalifiedaccess>]属性?

时间:2015-06-01 00:55:41

标签: f# ocaml discriminated-union

在F#程序中,我更喜欢使用

[<RequireQualifiedAccess>]
type MyType =
    | FirstOption of string
    | SecondOption of int

因此,在使用MyType的代码中,我被迫编写MyType.FirstOption而不是FirstOption。有没有办法在OCaml中强制执行此操作?

1 个答案:

答案 0 :(得分:4)

通过定义模块中的类型,您可以获得类似的效果。

$ ocaml
        OCaml version 4.02.1

# module MyType = struct
    type t = FirstOption of string | SecondOption of int
    end    ;;
module MyType : sig type t = FirstOption of string | SecondOption of int end
# MyType.FirstOption "abc";;
- : MyType.t = MyType.FirstOption "abc"
# FirstOption "abc";;
Error: Unbound constructor FirstOption
#

如果你这样做,那么类型的名称(如你所见)是MyType.t。

相关问题