我是OCaml模块的新手,我没有设法使用我自己的模块,但没有将“include”和“open”结合在一起。 我试图把签名放在单独的.mli文件中,但没有成功。OCaml模块:包含AND open?
在下面,我表示最低(不)工作的例子,我试图与
ocamlc -o main Robot.ml main.ml
编译什么我需要做的,只需要使用“打开”,或者只有“包括“,但不是他们两个?
文件 “Robot.ml”:
module type RobotSignature =
sig
val top: unit -> unit
end
module Robot =
struct
let top() =
begin
Printf.printf "top\n"
end
(* Should not be visible from the 'main' *)
let dummy() =
begin
Printf.printf "dummy\n"
end
end
文件 “main.ml”(不工作):
open Robot;;
top();
文件 “main.ml”(工作):
include Robot;;
open Robot;;
top();
我想你有问题的答案。您可能还想阅读[编译单元](http://caml.inria.fr/pub/docs/manual-ocaml/manual020.html)。但是,一旦你明白了'open'的作用,不要使用它,它会让你更难理解你的代码。 – 2012-04-03 17:57:41
那么我通常会同意,但在这种情况下,目标是提供一个简单的“机器人库”,教给初学者基本的编程(特别是,但不限于OCaml)。所以我宁愿尽可能避免使用Robot.top()语法。 – 2012-04-03 18:09:22
嗯,我认为这实际上会让初学者更加不可理解地渲染他们正在采取行动的对象。无论如何,您可能还想查看[open](http://caml.inria.fr/pub/docs/manual-ocaml/manual019.html#@manual.kwd170)和[include]( http://caml.inria.fr/pub/docs/manual-ocaml/manual019.html#@manual.kwd171)。 – 2012-04-03 18:12:13