module - 我可以用 1 个模块实现多种模块类型吗?

标签 module constraints ocaml reason

给定这些模块类型:

module type CodecTypes = {
  type t;
  type token;
};

module type Decode = {
  include CodecTypes;
  let decode: BsGenericParser.Parse.parser(token, t);
};

module type Encode = {
  include CodecTypes;
  let encode: t => list(token);
};

有没有办法在两种模块类型之间共享抽象类型ttoken? 我试过:

module type Codec = {
  include Encode;
  include Decode;
}

但是编译器提示名称冲突。

最佳答案

确实可以,使用 signature constraintsdestructive substitution :

module type Codec = {
  include Encode;
  include Decode with type t := t and type token := token;
}

请注意,您也可以使用 = 而不是 := 来编写签名约束,但这会产生相同的错误。它们的区别在于=是类型相等,要求两边的类型相等,而:=会替换(“破坏性替换”)上的类型左侧。在这种情况下,这意味着 Decode.t 被替换为 Encode.t

为了更好地说明这一点,请考虑以下示例:

module type Codec = {
  type u;
  include Encode with type t := u;
  include Decode with type t := u and type token := token;
}

这将导致以下模块类型:

module type Codec = {
  type u;
  type token;
  let encode: u => list(token);
  let decode: list(token) => u;
};

其中 t 根本不再出现,而是被 u 取代。

关于module - 我可以用 1 个模块实现多种模块类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61305696/

相关文章:

ocaml - 如何请求变量的类型类?

perl - 安装 Perl 模块是否需要付费许可证?

ocamlyacc 解析错误 : what token?

create_module - 为什么使用 copy_from_user?

iphone - 为什么约束不适用于 UIView 但适用于 UILabel?

swift - 以编程方式添加负约束

sql - Oracle SQL Developer - 添加外键约束

list - 如何可靠地比较列表的整数或浮点值?

ios - Trigger.io forge.topbar.setTint() 错误

node.js - 如何用node+express共享一个对象