问题描述
我需要生成一个与我传递的类型不同的值。这是我第一次写类似 ocaml 的东西,例如,在我熟悉的 Haskell 中,我会使用 Data.Generics。 我如何理解我需要使用装饰器和 ppx。我写了一个简单的例子
let recordHandler = (loc: Location.t,_recFlag: rec_flag,_t: type_declaration,fields: list(label_declaration)) => {
let (module Builder) = Ast_builder.make(loc);
let test = [%str
let schema: Schema = { name: "",_type: String,properties: [] }
]
let moduleExpr = Builder.pmod_structure(test);
[%str
module S = [%m moduleExpr]
]
}
let str_gen = (~loc,~path as _,(_rec: rec_flag,t: list(type_declaration))) => {
let t = List.hd(t)
switch t.ptype_kind {
| Ptype_record(fields) => recordHandler(loc,_rec,t,fields);
| _ => Location.raise_errorf(~loc,"schema is used only for records.");
};
};
let name = "my_schema";
let () = {
let str_type_decl = Deriving.Generator.make_noarg(str_gen);
Deriving.add(name,~str_type_decl) |> Deriving.ignore;
};
和
open ppxlib;
let _ = Driver.run_as_ppx_rewriter()
但在重写代码中使用
module User = {
@deriving(my_schema)
type my_typ = {
foo: int,};
};
我抓住了:
不支持架构
。当我将@deriving(my_schema) 更改为@deriving(abcd) 和@deriving(sschema) 时,我确保自己正确连接它。 我遇到了不同的错误
ppxlib.Deriving: 'abcd' 不是受支持的类型派生生成器。
我的最后一个实验是复制过去现有的库派生访问器。 ppx_accessor 我复制粘贴它并重命名为 accessors_2。我得到了同样的错误,比如实验。
不支持访问器_2
此外,我还没有找到示例“ppx rescript”。你能帮我么。 我做错了什么(全部,我知道)
解决方法
我在 article
中找到了答案Dropping support for custom PPXes 如 ppx_deriving(派生 属性现在被专门解释为 bs.deriving)