尝试进一步了解OCaml的接口/模块 [英] Try to further understanding the interface/module of OCaml
问题描述
我在OCaml中理解了 interfaces
和 module
的概念。
现在我明白如何使用它们了。
然而,我不明白如何充分利用它们。
例如,在Java中,假设我们有一个接口 Map
,并且我们还有 Hashtable
和 HashMap
,它们实现了 Map
。
我可以这样做:
Map m = new Hashtable();
m.put(key,value);
有一天,如果我改变了主意,可以改为 Hashmap
Map m = new Hashtable(); 为
Map m = new HashMap(); $ c $> c>,对吗?
但是我怎么能在Ocaml中轻松做到这一点? 例如,我有 MapSig
和'HashMap:MapSig 和Hashtable:MapSig code> in OCaml。
如何轻松更改实施?
我不认为我可以,因为在OCaml中我必须这样做:
let m = Hashtable.create( );;
Hashtable.put m键值;;
如果我我想用 我不仅在寻找一种对模块进行别名的方法,我还考虑了im的有效性例如,在上面的Java示例中,只有 HashMap
代替每一个 Hashtable
和 HashMap $ c
>。否则,Java编译器会报错。
$ b 编辑:
HashMap
已经实现了 Map
接口,我可以用 HashMap
Hashtable
但是如果我在OCaml中执行 module M = Hashtable
,并且 HashMap
不会跟随 MapSig
,而用<$ c替换 Hashtable
$ c> HashMap ,会发生什么?我认为编译器不会抱怨,对吗?
下面是一个示例,显示我认为您要求的内容:
#模块类型HASH = sig类型t val哈希值:t - > int end ;;
模块类型HASH = sig类型t val哈希值:t - > int end
#module I = struct type t = int let hash i = i end ;;
模块I:sig类型t = int val哈希:'a - > 'a end
#module J = struct type t = int end ;;
模块J:sig类型t = int结束
#模块M:HASH = I ;;
模块M:HASH
#模块N:HASH = J ;;
错误:签名不匹配:
模块不匹配:sig类型t = int结束不包含在HASH
字段`hash'是必需的但未提供
额外的:HASH
指定模块必须匹配HASH签名(它也限制了它的签名)。
就像一面评论,我相信OCaml模块系统以其表现力而闻名于世(至少在模块系统界)。我仍然是初学者,但值得研究。
I understand in OCaml there are concepts of interfaces
and module
.
And I understand how to use them now.
However, what I don't understand is how to fully utilise them.
For example, in Java, let's say we have a interface Map
and we also have Hashtable
and HashMap
that implement Map
.
In code, I can do like:
Map m = new Hashtable();
m.put("key", value);
Someday, if I change my mind, I can change to Hashmap
very quickly by changing Map m = new Hashtable();
to Map m = new HashMap();
, right?
But how can I easily do that in Ocaml?
For example, I have MapSig
and 'HashMap:MapSigand "Hashtable:MapSig
in OCaml.
How can I change the implementation easily?
I don't think I can because in OCaml I have to do like:
let m = Hashtable.create ();;
Hashtable.put m key value;;
if I want to use HashMap
instead, I have to replace every Hashtable
with HashMap
in the code, right?
Edit:
I am not only seeking a way to make a alias to modules. I also consider the validity of implementations, i.e., whether the implementation follow the desired interface.
For example, in above Java example, only if HashMap
has implemented Map
interface, I can replace Hashtable
with HashMap
. otherwise, Java compiler will complain.
but if I do module M = Hashtable
in OCaml, and if HashMap
does not follow MapSig
and I replace Hashtable
with HashMap
, what will happen? I think compiler won't complain, right?
Here's an example that shows what I think you're asking for:
# module type HASH = sig type t val hash : t -> int end ;;
module type HASH = sig type t val hash : t -> int end
# module I = struct type t = int let hash i = i end ;;
module I : sig type t = int val hash : 'a -> 'a end
# module J = struct type t = int end ;;
module J : sig type t = int end
# module M : HASH = I ;;
module M : HASH
# module N : HASH = J ;;
Error: Signature mismatch:
Modules do not match: sig type t = int end is not included in HASH
The field `hash' is required but not provided
The extra ": HASH
" specifies that the module must match the HASH signature (and it also restricts it to that signature).
Just as a side comment, I believe the OCaml module system is world famous for its expressivity (at least in module system circles). I'm still a beginner at it, but it is worth studying.
这篇关于尝试进一步了解OCaml的接口/模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!