orbitz
January 14, 2022, 10:33am
1
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
Kakadu
January 14, 2022, 10:40am
2
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 = ()
orbitz
January 14, 2022, 11:00am
3
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β¦
Kakadu
January 14, 2022, 11:04am
4
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
orbitz
January 14, 2022, 3:45pm
6
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.
orbitz
January 15, 2022, 12:42pm
8
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 = ()