首页 > 解决方案 > 如何为多态对象类型定义抽象(不透明)接口?

问题描述

原因机器学习

module type T = {
  type t('a); // Does not work
  type b; // Works
};

module A: T = {
  type t('a) = {.. b: bool} as 'a;
  type b = bool;
};
module B: T = {
  type t('a) = {.. c: int} as 'a;
  type b = int;
};

奥卡姆

module type T  = sig
  type 'a t /* Doesn't work */
  type b /* Works */
end
module A : T = struct type 'a t = < b :bool  ;.. > as 'a
                      type b = bool end 
module B : T = struct type 'a t = < c :int  ;.. > as 'a
                      type b = int end  

如何定义模块类型 A t('a) 使其抽象但与实现中的开放多态对象类型兼容?

标签: ocamlreasonrescript

解决方案


目前尚不清楚它是否真的有用,但您需要在模块类型中显式添加约束:

module type TA  = sig
  type 'a t constraint 'a = < b:bool; .. >
end
module A : TA = struct
  type 'a t = < b :bool  ;.. > as 'a
end

推荐阅读