Functional programming OCaml中“with type”与函子的区别
假设以下模块:Functional programming OCaml中“with type”与函子的区别,functional-programming,ocaml,Functional Programming,Ocaml,假设以下模块: module type A = sig type a type f = r:a -> unit Lwt.t end module type AA = A with type a = B.t 我不能将模块A参数化(将其转换为接受B.t作为参数的函子)吗 module type A (b: B.t) = sig type f = r:b -> unit Lwt.t end module type AA = A(B.t) 这些是等价物吗?为什么有两种方法
module type A = sig
type a
type f = r:a -> unit Lwt.t
end
module type AA = A with type a = B.t
我不能将模块A
参数化(将其转换为接受B.t
作为参数的函子)吗
module type A (b: B.t) = sig
type f = r:b -> unit Lwt.t
end
module type AA = A(B.t)
这些是等价物吗?为什么有两种方法做同一件事
这些是等价物吗?为什么有两种方法做同一件事
没有,他们也没有。你的“函子”是无效的,你可以很容易地找到它自己尝试
函子和模约束(即,类型为…
)在不同的域中工作。函子处理实现,模块约束处理签名/类型。此外,模块约束接受现有签名并向其添加信息(约束),而函子接受模块实现作为参数,但不扩展它,或者需要输入和输出签名彼此之间具有任何关系
如前所述,可以使用include
,使用函子以一般方式扩展实现。但由于参数的签名是静态的,因此其可重用性和有用性受到限制:
module A (B1: B) = struct
include B
let x = 42
end