Is there an OCaml equivalent of the [<RequireQualifiedAccess>] attribute for DUs in F#?
Answers
In F# programs I prefer to use
[<RequireQualifiedAccess>]
type MyType =
| FirstOption of string
| SecondOption of int
so that in code that uses MyType I am forced to write MyType.FirstOption instead of just FirstOption. Is there any way to force this in OCaml?
Answers
You can get a similar effect by defining the type in a module.
$ 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
#
If you do it this way, the name of the type (as you can see) is MyType.t.
コメント