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.

This can be used to build all sorts of things, e.g. building a catalog form for a record type, or building a creator function which uses an applicative or monad like deferred to create the content for each field, perhaps useful when combined with Quickcheck.Generator.

e.g.

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.map
    (G.both (labelled_string Field.forename) (G.both (labelled_string Field.surname) Date.gen)
       ~f:(fun (forename, (surname, birthday)) -> { forename; surname; birthday; })
type 'a applicative

These types have to be exposed so that the typechecker can tell that the use of Fields.make_creator is well-typed.

val field : 'field applicative -> ('record, 'field) Core_kernel.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 supplies the applicative terms to use for each field of the record. It performs the mapping and combining of these terms automatically.

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