类型定义

时间:2016-05-23 13:34:37

标签: ocaml

我想在我的代码中使用日期和时间,所以我使用opam加载了Calendar Lib。我有一段简单的代码来演示问题(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构造函数。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您要使用的函数和数据类型位于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