我是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