Adding attributes using ppxlib.metaquot

Is it possible to add attributes to values using ppxlib.metaquot? I am doing code generation that I will print out via Pprintast and I what to use [@@deriving show] in that output: the following is not working:

[%stri type t = string [@@deriving show]]

With the error [] expected.

The following compiles and runs

[%stri type t = string [@deriving show]]

But that is not the correct kind of attribute

It looks like first approach works in toplevel…

#require "ppxlib";;
#require "ppxlib.metaquot";;
...
Format.printf "%a\n" Ppxlib.Pprintast.structure_item (let loc = Location.none in [%stri type t = string [@@deriving show]]);;
type t = string[@@deriving show]
- : unit = ()

I can’t even #require "ppxlib";; in utop, I get:

utop # #require "ppxlib";;
Error: Reference to undefined global `Location'

Not sure what the cause of that is…

I’m using OCaml 4.12.1 and ppxlib 0.22.2

You need to use utop-full to be able to use the compiler libraries in the REPL.

2 Likes

With help of @octachron I managed to do this on my machine and it worked. I’m not sure what the difference is between doing this in a file vs the top-leve.

FYI, I’ve tried to run it from a compiled program and it works too.

This looks to be related to pulling in ppx_deriving and order seems to matter:

─( 13:41:16 )─< command 0 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # #require "ppx_deriving";;
─( 13:41:17 )─< command 1 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # #require "ppxlib";;
─( 13:41:22 )─< command 2 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # #require "ppxlib.metaquot";;
─( 13:41:26 )─< command 3 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Format.printf "%a\n" Ppxlib.Pprintast.structure_item (let loc = Location.none in [%stri type t = string [@@deriving show]]);;
Error: Ppxlib.Deriving: 'show' is not a supported type deriving generator

and

─( 13:41:40 )─< command 0 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # #require "ppxlib";;
─( 13:41:40 )─< command 1 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # #require "ppxlib.metaquot";;
─( 13:41:45 )─< command 2 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # #require "ppx_deriving";;
─( 13:41:49 )─< command 3 >─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Format.printf "%a\n" Ppxlib.Pprintast.structure_item (let loc = Location.none in [%stri type t = string [@@deriving show]]);;
type t = string[@@deriving show]
- : unit = ()