running a ppx extension based on type_conv

14 views
Skip to first unread message

Ashish Agarwal

unread,
Oct 2, 2015, 3:13:33 PM10/2/15
to ocaml...@googlegroups.com
What are the requirements of Type_conv.add? I'm doing this:

let () =
  Type_conv.add "mycodegen"
  |> Type_conv.ignore

which I was hoping would be a noop. However, I get the error:

[%%ocaml.error
  let _ = "Type declarations in structures not supported by deriver mycodegen"
  let _ = ""]
File "_none_", line 1:
Type declarations in structures not supported by deriver mycodegen

I've tried adding the following str_type_decl, but I get the same error.

let str_type_decl : (structure, rec_flag * type_declaration list) Type_conv.Generator.t =
  Type_conv.Generator.make_noarg (fun ~loc:_ ~path:_ _ ->
      Type_conv.Generator_result.make_just_after [%str let x = 42]
    )

Jeremie Dimino

unread,
Oct 5, 2015, 7:08:21 AM10/5/15
to ocaml...@googlegroups.com
On Fri, Oct 2, 2015 at 8:13 PM, Ashish Agarwal <agarw...@gmail.com> wrote:
What are the requirements of Type_conv.add? I'm doing this:

let () =
  Type_conv.add "mycodegen"
  |> Type_conv.ignore

​Type-conv enables the deriver when a generator is available. You​
 
​need to fill the optional arguments of `Type_conv.add`​ to set generators. A no-op derivier is:

  let gen = Type_conv.Generator.make_noarg (fun ~loc:_ ~path:_ -> [])

  let () =
    Type_conv.add "mycodegen"
      ~str_type_decl:gen
      ~sig_type_decl:gen
      ~str_exception:gen
      ~sig_exception:gen
      ~str_type_ext:gen
      ~sig_type_ext:gen
    |> Type_conv.ignore

It's better to fail if no generator was prodived. This way the user doesn't waste time wondering why putting [@@deriving sexp] on a type extension has no effect. And it's always possible to explicitly provide a no-op generator if that's what one want.

I've tried adding the following str_type_decl, but I get the same error.

let str_type_decl : (structure, rec_flag * type_declaration list) Type_conv.Generator.t =
  Type_conv.Generator.make_noarg (fun ~loc:_ ~path:_ _ ->
      Type_conv.Generator_result.make_just_after [%str let x = 42]
    )

​This creates a generator to use with `Type_conv.add`, ​
 
​but registers nothing.
--
Jeremie

Ashish Agarwal

unread,
Oct 5, 2015, 3:21:16 PM10/5/15
to ocaml...@googlegroups.com
On Mon, Oct 5, 2015 at 7:08 AM, Jeremie Dimino <jdi...@janestreet.com> wrote:

A no-op derivier is:

  let gen = Type_conv.Generator.make_noarg (fun ~loc:_ ~path:_ -> [])

Thanks, I get it now.

 

  let () =
    Type_conv.add "mycodegen"
      ~str_type_decl:gen
      ~sig_type_decl:gen
      ~str_exception:gen
      ~sig_exception:gen
      ~str_type_ext:gen
      ~sig_type_ext:gen
    |> Type_conv.ignore

This doesn't typecheck (need to define a different gen for each arg), but I get your point. And I see that deliberately giving a no-op deriver is probably wrong. We should rather omit it to get informative error messages.


I've tried adding the following str_type_decl, but I get the same error.

let str_type_decl : (structure, rec_flag * type_declaration list) Type_conv.Generator.t =
  Type_conv.Generator.make_noarg (fun ~loc:_ ~path:_ _ ->
      Type_conv.Generator_result.make_just_after [%str let x = 42]
    )

​This creates a generator to use with `Type_conv.add`, ​
 
​but registers nothing.

Right, I was unclear. I meant I defined this *and* passed it as an arg to Type_conv.add. But now it somehow is working. I must have been doing something else wrong, probably not calling the compiler with the correct -ppx argument.

Thanks for all the help.
Reply all
Reply to author
Forward
0 new messages