Ocaml 如何编写自定义ppx装饰程序来重新编写?
我需要生成一个与传递的类型不同的值。这是我第一次像在ocaml上写东西,例如,在熟悉的me haskell中,我会使用Data.Generics。 我如何理解我需要使用decorator和ppx。我写了一个简单的例子Ocaml 如何编写自定义ppx装饰程序来重新编写?,ocaml,reason,deriving,rescript,ppx,Ocaml,Reason,Deriving,Rescript,Ppx,我需要生成一个与传递的类型不同的值。这是我第一次像在ocaml上写东西,例如,在熟悉的me haskell中,我会使用Data.Generics。 我如何理解我需要使用decorator和ppx。我写了一个简单的例子 let recordHandler = (loc: Location.t, _recFlag: rec_flag, _t: type_declaration, fields: list(label_declaration)) => { let (module Builde
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;
};
及
但在重新编写代码中使用
module User = {
@deriving(my_schema)
type my_typ = {
foo: int,
};
};
我发现:
架构不受支持
。当我把@deriving(我的_模式)改为@deriving(abcd)和@deriving(sschema)时,我确保我能正确地连接它。
我犯了不同的错误
Ppxlib.派生:“abcd”不是受支持的类型派生生成器
我的最后一个实验是复制现有的库派生访问器。
我复制粘贴了它,并为accessors_2重命名了它。我得到了和实验一样的错误
不支持访问器_2
此外,我还没有找到“ppx重新描述”的例子。你能帮帮我吗。
我做错了什么(我都知道)我在答案中找到了答案 例如ppx_推导(推导 属性现在专门解释为bs.deriving) 交叉张贴。请披露,如果你是交叉张贴。
module User = {
@deriving(my_schema)
type my_typ = {
foo: int,
};
};