OCaml 仿函数的语法糖

标签 syntax ocaml functor

为什么会这样:

module type ENTRY = sig type t end       
module type LOG = functor (E : ENTRY) -> sig type t end 

这是一个有效的 LOG 实现

module Log :LOG  = functor (LogEntry : ENTRY) -> 
  struct  type t = LogEntry.t list end

但这不是

module Log (LogEntry: ENTRY) :LOG  = struct
type t = LogEntry.t list end

Error: Signature mismatch:                                                                                                             
Modules do not match: sig type t = LogEntry.t list end is not included in LOG

如果我从 Log 的两个定义中删除 sig 标签 (:LOG),那么它们将返回相同的类型,因为它们只是语法糖[1]

[1] http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora132.html

最佳答案

错误消息令人困惑,但第一个示例通过而第二个示例失败的原因实际上非常简单。比较:

type entry = int
type log = int -> string
let log : log = fun s -> string_of_int s

let log (s : entry) : log = string_of_int s

模块情况下的错误消息表明模块字段未包含在仿函数中,因为未应用的仿函数没有字段。

预计到达时间:从逻辑上讲,仿函数不能有字段:函数/仿函数与数据结构/模块相比是“另一种野兽”。 -- 这使错误消息变得困惑,听起来我们被要求引入一个字段,尽管它已经存在于仿函数的结果中。

关于OCaml 仿函数的语法糖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337229/

相关文章:

java - 我需要知道这两句话在编程中是否相同

mysql - 如何在 MySQL 脚本中透视单个表?

python - 在单个语句中在 python 中多次测试单个变量

string - OCaml:问题处理从文件中读取的字符串

C++ 结构排序错误

lambda - lisp 中的 bool 仿函数

ios - 如何访问 Cocoa Touch 类中 UIStepper 的值

OCaml,扩展了一个程序,如 Emacs

ocaml - Standard ML 的模块系统和 OCaml 模块系统有什么区别(如果有的话)?

C++ 仿函数状态和首选项