【问题标题】:How to write custom ppx decorator to rescript?如何编写自定义 ppx 装饰器来重写?
【发布时间】:2021-03-15 21:37:42
【问题描述】:

我需要生成一个与我传递的类型不同类型的值。这是我第一次写类似 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()

但是在rescript代码中使用

module User = {
  @deriving(my_schema)
  type my_typ = {
    foo: int,
  };
};

我抓住了:

不支持架构

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

Ppxlib.Deriving: 'abcd' 不是受支持的类型派生生成器。

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

accessors_2 不受支持

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

【问题讨论】:

标签: ocaml reason deriving rescript ppx


【解决方案1】:

我在article找到了答案

Dropping support for custom PPXes如ppx_deriving(派生 属性现在被专门解释为 bs.deriving)

【讨论】:

    猜你喜欢
    • 2011-07-25
    • 2021-01-28
    • 2018-12-10
    • 1970-01-01
    • 2015-03-22
    • 2012-11-10
    • 2015-10-22
    • 2015-08-31
    • 2010-09-15
    相关资源
    最近更新 更多