Ocaml 是否可以使用模块动态创建多态变量类型?
我正在尝试组织并重用我的reasonML代码。我的模型模块类型如下所示:Ocaml 是否可以使用模块动态创建多态变量类型?,ocaml,variant,strong-typing,reason,polymorphic-variants,Ocaml,Variant,Strong Typing,Reason,Polymorphic Variants,我正在尝试组织并重用我的reasonML代码。我的模型模块类型如下所示: module Diet = { type schemaType = [`DietSchema]; type idType = [`DietId(UUID.t)]; let schema = `DietSchema; type idAsType('a) = [> | idType] as 'a; }; module Ingredient = { type schemaType = [
module Diet = {
type schemaType = [`DietSchema];
type idType = [`DietId(UUID.t)];
let schema = `DietSchema;
type idAsType('a) = [> | idType] as 'a;
};
module Ingredient = {
type schemaType = [`IngredientSchema];
type idType = [`IngredientId(UUID.t)];
let schema = `IngredientSchema;
type idAsType('a) = [> | idType] as 'a;
};
module Restriction = {
type schemaType = [`RestrictionSchema];
type idType = [`RestrictionId(UUID.t)];
let schema = `RestrictionSchema;
type idAsType('a) = [> | idType] as 'a;
};
我想从idType
s和schemaType
s生成一个类型和函数
例如:
type modelIdType = [
| Diet.idType
| Restriction.idType
| Ingredient.idType
];
type schemaType = [
| Diet.schemaType
| Restriction.schemaType
| Ingredient.schemaType
];
let modelIdToIdFunction = (recordIdType): (schemaType, UUID.t) =>
switch (recordIdType) {
| `DietId(uuid) => (Diet.schema, uuid)
| `RestrictionId(uuid) => (Restriction.schema, uuid)
| `IngredientId(uuid) => (Ingredient.schema, uuid)
};
因此,我试图使用一个函子来构造一个模块,该函子将每个模式传递给
module Diet : SchemaType = {
/* ... */
};
module type SchemaType {
type schemaType;
type idType;
let schema: [> schemaType];
type idAsType('a) = [> | idType] as 'a;
};
module ProcessSchema = (
Schema : SchemaType,
PrevFullSchema : FullSchema
) : (FullSchema) => {
type id = [> Schema.idType' | PrevFullSchema.id'('a)] as 'a;
/* type id = [PrevFullSchema.openId(PrevFullSchema.id) | Schema.idType]; */
/* type schema = [PrevFullSchema.schema | Schema.schema]; */
/* type openSchema = [PrevFullSchema.schema | Schema.schema]; */
};
上面的代码不起作用。我在向顶部的模型模块添加模块类型时遇到问题。我还尝试了一个SchemaType
模块类型,但一直在点击类型idType不是多态变量类型
,当时我希望每个模型都有不同的多态变量类型
所以总的来说,我想知道是否有可能创建一个多态变量类型,可以使用模块和函子创建或扩展它
如果没有,是否可以使用“模块列表”构造多态变体类型
感谢您提供了可以使用可扩展变量类型的类型。但是对于给定模块列表的modelIdToIdFunction函数,我认为您只能在列表中搜索,而不会进行缩放
您应该使用每个模块的ID扩展uuid,以便可以从模块ID到列表中的模块创建一个查找表,以便快速访问。2002年有人问过类似的问题。根据一位OCaml语言开发人员的说法,不可能像这样动态扩展多态变量类型:。相关比特: 函子定义被拒绝,因为 “M.t型不是多态变体类型” 有解决办法吗 我不知道。多态变体扩展仅适用于已知的 关闭变量类型,否则它将不健全
文章的其余部分提出了一个建议,归结起来就是在不同的标记中捕获新的变体类型,但这同样不适用于使用functor动态“添加”类型的用例。这“显然”在什么方面不起作用?它不会编译,但这是因为缺少了几个定义,并且既不清楚您要做什么,也不清楚您遇到了什么问题。你能回答一个问题吗?不过,答案很可能是否定的。动态创建类型不是静态类型语言通常可以做的事情。“静态”表示在编译时检查类型,“动态”表示在运行时检查类型。如果在检查完成后可以创建类型,那就不太安全了。但也许你在这里用的是另一种意义上的“动态”?是的,上面的例子没有编译,我想包含它可能没有用。这是试图构建一个函子,为多态变量生成一个类型。我想我也用错了词。我想我所说的“动态”是指“可扩展的”。我想通过添加更多多态变体来扩展类型。在哪一点上?你打算怎么处理他们?多态变体是结构性的,所以你不能真正“扩展”它们。当你给它命名时,它实际上只是右边类型的别名。多态变量也支持子类型,因此您可以指定一个多态变量类型,该类型表示“至少这些构造函数”:
[>`this | ` that]
,但这意味着一个类型变量,也就是说,它实际上是[>`this | ` that]作为一个。还有一些更像普通变量,但您必须包含一个通配符模式,这意味着你失去了详尽的检查。我还没有时间来实现,但似乎你是正确的可扩展变量类型似乎是解决方案。稍后,我将使用SchemaType
和ProcessSchema
的实现进行更新,或者如果有人能向我展示它们的样子,我会接受。我会把这个给你,因为我不想浪费赏金积分。谢谢