来自类型定义的ocaml未绑定构造函数类型错误

ocaml unbound constructor type error from type definition

我想在我的代码中使用日期和时间,所以我使用 opam 加载了日历库。我有一段简单的代码演示了这个问题 (example.ml):

open CalendarLib

type datefun = date -> int

let run_datefun (f : datefun) (d : date) = (f d)

let () =
  let mydate = make 2016 5 23 in
  printf "Day of week = %i" run_datefun days_in_month mydate

据我所知,Calendar days_in_month 方法的类型签名为 date -> int

当我尝试编译此代码 (corebuild -pkg calendar example.byte) 时,出现以下错误:

File "example.ml", line 3, characters 15-19:
Error: Unbound type constructor date

在我看来,编译器正在寻找日期类型的日期构造函数。

我做错了什么?

您要使用的函数和数据类型在 Date module 中,因此改写我们得到的代码(我还冒昧地重写了输出短语并插入了缺少的括号):

open CalendarLib

type datefun = Date.t -> int

let run_datefun (f : datefun) (d : Date.t) = (f d)

let () =
  let mydate = Date.make 2016 5 23 in
  Printf.printf "# of days in current month = %i\n" (run_datefun Date.days_in_month mydate)

小测试(顺便说一句,你不需要 corebuild):

$ ocamlbuild -pkg calendar example.ml example.byte
Finished, 3 targets (3 cached) in 00:00:00.

$ _build/calendar.byte
# of days in current month = 31