Ppxlib usage help: record creation

The ppxlib tutorial is excellent, but the API documentation is unfortunately lacking.

I’m trying to create a pattern for getting the elements out of

let x = [%update {x with foo=bar; baz=fooz}] in 

To break down the record update, I use Ast_pattern.pexp_record. However, at this point I’m at a loss. The type of pexp_record is

((Ppxlib.longident_loc * expression) list, 'a, 'b) t -> (expression option, 'b, 'c) t -> (expression, 'a, 'c) t

Which combinators do I use to further take apart this expression?

The API should be straitforward after some meditaion :slight_smile: Use many for lists, and double asterisk for pairs.

#use "topfind";;
#require "ppxlib";;
#require "ppxlib.metaquot";;

open Ppxlib.Ast_builder.Default
open Format

let loc = Location.none;;
let record = [%expr {x with foo=bar; baz=fooz}];;

let pp_expr ppf e =
  Format.fprintf ppf "%a" (Ocaml_common.Pprintast.expression) e

let () = Ppxlib.Ast_pattern.(
    let pfield = (__ ** __) |> map2 ~f:(fun a b -> (a,b)) in
    parse (pexp_record (many pfield) __)
  )
  loc
  record
  (fun fields b ->
    List.iter (fun (fname,b) ->
      let _ : Ppxlib__.Import.longident_loc = fname in
      printf "? = %a\n" (pp_expr) b)fields;
    printf "%a\n%!" (pp_print_option pp_expr) b)
? = bar
? = fooz
x

2 Likes