package record_builder

  1. Overview
  2. Docs

Modules of this type are used to traverse a record using a specific applicative. They can be made using the functor Record_builder.Make.

For example, we could traverse a record to create a quickcheck generator for it.

type t = {
  forename: string;
  surname: string;
  birthday: Date.t;
} [@@deriving fields]

module G = Quickcheck.Generator

let form : t G.t =
  let module B = Record_builder.Make(G) in
  let labelled_string field =
    B.field (G.map String.gen ~f:(fun str ->
      sprintf "%s:%s" (Field.name field) str))
  in
  B.build_for_record (
    Fields.make_creator
      ~forename:labelled_string
      ~surname:labelled_string
      ~birthday:(B.field Date.gen))

Is equivalent to:

let form : t G.t =
  let labelled_string field =
    G.map String.gen ~f:(fun str ->
      sprintf "%s:%s" (Field.name field) str)
  in
  G.both (labelled_string Field.forename)
    (G.both (labelled_string Field.surname) Date.gen)
  |> G.map ~f:(fun (forename, (surname, birthday)) ->
    { forename; surname; birthday; })
type 'a applicative

The types used internally for building records by folding over fields — you shouldn't need to know them, but they must be exposed to allow type-checking to succeed.

val field : 'field applicative -> ('record, 'field) Base.Field.t -> ('field, _, _, _) Make_creator_types.handle_one_field

Supply the term for one field.

The type of this function is designed to match up with Fields.make_creator (see the example).

val build_for_record : ('record, _, _) Make_creator_types.handle_all_fields -> 'record applicative

Build the overarching applicative for the whole record.

This takes a partial application of Fields.make_creator as its argument, which should supply no initial value but use field to supply a term for every field of the record.

The type of this is designed to match up with Fields.make_creator (see the example).