我正在尝试组织和重用我的 reasonML 代码。我的模型模块类型如下所示:
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
模块类型,但一直在点击The type idType is not a polymorphic variant type
,当我希望每个模型都有不同的多态变量类型时。
所以总的来说,我想知道是否可以创建一个可以使用模块和仿函数创建或扩展的多态变体类型?
如果不是,是否可以使用“模块列表”构造多态变体类型?
谢谢