Sexplib: Serializing one field of a record without field name

I’m serializing just 1 field of my record like this right now

type ('a, 'b) tagged =
  { value : 'a
  ; tag : 'b [@sexp_drop_if fun _ -> true]
  }
[@@deriving sexp]

This comes out as (value instr32) but is there a way to make it come out as just instr32, omitting the value?

Alternatively, how do I write a custom converter when the field types aren’t known in advance?

The functions from @deriving sexp will always include field names, but you can just write a conversion function manually:

type ('a, 'b) tagged =
  { value : 'a
  ; tag : 'b
  }
let sexp_of_tagged sexp_of_a _ { value; tag = _ } = sexp_of_a value

An other common pattern, when you have a complicated structure but want a lighter syntax for a common case, is something like:

type t =
  | A of string
  | B of ...
[@@deriving sexp]
let sexp_of_t t = match t with A s -> sexp_of_string s | _ -> sexp_of_t t
let t_of_sexp sexp = match sexp with Atom _ -> A (string_of_sexp sexp) | _ -> t_of_sexp sexp
2 Likes