Debian Codegraph @

README.md

raw symbols SCIP
1 ppx_sexp_value
2 ==============
3
4 A ppx rewriter that simplifies building s-expressions from ocaml values.
5
6 Basic use
7 ---------
8
9 The building block of this preprocessor is the extension:
10
11 ```ocaml
12 [%sexp expr]
13 ```
14
15 in expressions. It evaluates to an s-expression. This is done by
16 recursing down the expression and converting all data constructors
17 into s-expressions. If an expression with a type annotation is found,
18 then the type is used to convert to s-expression. Expressions that are
19 neither data constructors nor annotated with a type will be rejected.
20
21 For instance:
22
23 ```ocaml
24 [%sexp { a = "hello" ; b = (Time.now () : Time.t) } ]
25 ```
26
27 will be preprocessed into:
28
29 ```ocaml
30 List [List [Atom "a"; Atom "hello"];
31 List [Atom "b"; [%sexp_of: Time.t] (Time.now ())];
32 ]
33 ```
34
35 This does not require a record with fields a and b to exist (and if
36 one does exist, its sexp_of function will be ignored unless a type
37 annotation is added around the record).
38 One can annotate a record field with the attribute `[@sexp.option]`
39 or with `[@sexp.omit_nil]` to achieve the same result as when using
40 sexplib. They both have the same behavior inside tuples.
41
42 Variant, polymorphic variants, tuples and lists are supported as
43 well. Variants are analogous to records in that a type containing the
44 variant does not have to exist unless a type annotation is added to
45 the variant.
46
47 Expressions with their evaluations
48 ----------------------------------
49
50 It is sometimes convenient to include the expression itself in the
51 s-expression. This is especially true for debugging, as it avoids
52 having to think of a label for a value; one can simply use the
53 expression itself.
54
55 Ppx\_sexp\_value allows this by reserving the `~~` operator. Inside
56 `[%sexp]`, `~~<expr>` is the same as `("<expr>", <expr>)`, where the
57 type annotation in `<expr>` is stripped off in the s-expression
58 (`~~<expr>` isn't allowed if `<expr>` is a data constructor).
59
60 For instance:
61
62 ```ocaml
63 [%sexp (~~(x : int), ~~(y + z : int), "literal") ]
64 ```
65
66 will be preprocessed into:
67
68 ```ocaml
69 List [List [Atom "x"; [%sexp_of: int] x];
70 List [Atom "y + z"; [%sexp_of: int] (y + z)];
71 [%sexp_of: string] "literal";
72 ]
73 ```
74 Recommended use for errors
75 --------------------------
76
77 This extension is primarily intended to build better errors, by making
78 building errors easier and more readable, particularly when using
79 records to add context:
80
81 ```ocaml
82 try Unix.rename ~src:tmpfile ~dst
83 with exn ->
84 raise_s
85 [%sexp
86 "Error while renaming file",
87 { source = (tmpfile : string)
88 ; dest = (dst : string)
89 ; exn = (exn : exn )
90 }
91 ]
92 ```
93
94 Eagerness
95 --------
96
97 The extension `[%lazy_sexp]` is additionally provided, which just wraps
98 the generated code in `[lazy]` to delay computing and allocating of
99 possibly large sexps. This is intended to be used with, for example,
100 `[Error.of_lazy_sexp]`, where the error messages may be large, but
101 are typically not used:
102
103 ```ocaml
104 let execute_query_exn ~database ~query =
105 Database.execute_query ~database ~query
106 |> Option.value_exn
107 ~error:
108 (Error.of_lazy_sexp
109 [%lazy_sexp ("Query failed", { database : Database.t; query : Query.t })])
110 ```