package async

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type nonrec 'a t = 'a -> unit Async_kernel.Deferred.t
module type S = sig ... end
module type S1 = sig ... end
module type S2 = sig ... end
module type S3 = sig ... end
val check_field : 'a -> 'b t -> unit Async_kernel.Deferred.t -> ('a, 'b) Core_kernel.Field.t -> unit Async_kernel.Deferred.t

check_field can be used to check record fields when using [@@deriving fields]. Idiomatic usage looks like:

type t = { foo : Foo.t ; bar : Bar.t }
[@@deriving fields]

let invariant t =
  Invariant.Async.invariant [%here] t [%sexp_of: t] (fun () ->
    let check inv = Invariant.Async.check_field t inv in
    Fields.fold ~init:(return ())
      ~foo: (check Foo.invariant)
      ~bar: (check Bar.invariant) 

When some fields have synchronous invariants, or do not need to be checked, it may be useful to define a second wrapper around check_field:

type t = { foo : Foo.t ; bar : Bar.t ; quux : Quux.t }
[@@deriving fields]

let invariant t =
  Invariant.Async.invariant [%here] t [%sexp_of: t] (fun () ->
    let check' inv = Invariant.Async.check_field t inv in
    let check inv = check' (fun x -> inv x; return ()) in
    Fields.fold ~init:(return ())
      ~foo:  (check' Foo.invariant)
      ~bar:  (check  Bar.invariant)
      ~quux: (check  ignore) 
OCaml

Innovation. Community. Security.