gpt4 book ai didi

ocaml - 如何编写自定义 ppx 装饰器来重写脚本?

转载 作者:行者123 更新时间:2023-12-05 04:52:02 25 4
gpt4 key购买 nike

我需要生成一个与我传递的类型不同的值。这是我第一次写类似 ocaml 的东西,例如,在熟悉的 me 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,
};
};

我捕获了:

schema is not supported

.当我将 @deriving(my_schema) 更改为 @deriving(abcd) 和 @deriving(sschema) 时,我确保自己正确连接它。我得到了不同的错误

Ppxlib.Deriving: 'abcd' is not a supported type deriving generator.

我的最后一个实验是复制过去的现有库派生访问器。 ppx_accessor我复制粘贴它并重命名为 accessors_2。我得到了同样的错误,比如实验。

accessors_2 is not supported

此外,我还没有找到示例“ppx rescript”。你能帮我么。我做错了什么(所有,我知道)

最佳答案

我在 article 中找到了答案

Dropping support for custom PPXes such as ppx_deriving (the derivingattribute is now exclusively interpreted as bs.deriving)

关于ocaml - 如何编写自定义 ppx 装饰器来重写脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66610786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com