Help: simplify this code

  type source_loc = { row : int; col : int } [@@deriving compare, equal]

  let conv_loc (pos : Sexplib.Src_pos.Relative.t) : source_loc =
    match pos with
    | { row; col } -> { row; col }

Why I am defining this in the first place: for the [@@deriving compare, equal]

the conv_loc code seems convoluted; is there a way to simplify this function ?


You can re-export types to let ppx derivers works on the full type definition while keeping the two types equal:

type source_loc = Sexplib.Src_pos.Relative.t = { row : int; col : int }
  [@@deriving compare, equal]

Other things being equal, this should be the same as:

let conv_loc ({ row; col } : Sexplib.Src_pos.Relative.t) : source_loc = { row; col }

This looks like a gap in my OCaml knowledge. Where did you learn this ?

Since this is not that intuitive for people not (very?) familiar with ppxs and types definitions, this is explained in ppx_deriving documentation: GitHub - ocaml-ppx/ppx_deriving: Type-driven code generation for OCaml .

1 Like