package diff

  1. Overview
  2. Docs

Module Diff.FieldSource

This module contains what might be described as faux lenses. This includes support for composition, getting, setting, etc.

Sourcetype (_, _) t = ..

An open type that represents a field on a record.

For example,

type t = {
  x : int
}

would be described as X : (t, int) field.

Sourcetype getter = {
  1. f : 'a 'b. 'a -> ('a, 'b) t -> 'b option;
}

A polymorphic function that returns the field's value from the type.

Sourcetype setter = {
  1. f : 'a 'b. 'a -> ('a, 'b) t -> 'b -> 'a option;
}

A polymorphic function that returns the type with the field set to the value.

Sourcetype error =
  1. | Unknown_field : (_, _) t -> error
  2. | Getter_invalid : (_, _) t -> error
  3. | Setter_invalid : (_, _) t -> error
Sourceexception Diff_field of error
Sourceval name : ('a, 'b) t -> string option

name f returns the previously registered name for f, if one exists.

Sourceval cons : ('a, 'b) t -> ('b, 'c) t -> ('a, 'c) t

cons l r is a field that first indexes l and then r.

Sourceval opt_map : ('a, 'b) t -> ('a option, 'b option) t

opt_map f turns f into an optional field.

Sourceval opt_bind : ('a, 'b option) t -> ('a option, 'b option) t

opt_map f monadically binds f into an optional field.

Sourceval register : ?name:string -> ('a, 'b) t -> getter -> setter -> unit

register ?name field getter setter registers getter and setter (and optionally name) to field.

Sourceval pp : Format.formatter -> ('a, 'b) t -> unit
Sourceval pp_error : Format.formatter -> error -> unit
Sourcemodule Infix : sig ... end
Sourceval get : 'a -> ('a, 'b) t -> 'b

get v field gets field from v, raising Diff_field error on exception.

Sourceval get_opt : 'a -> ('a, 'b) t -> 'b option

get_opt v field gets field from v, returning None on exception.

Sourceval get_res : 'a -> ('a, 'b) t -> ('b, [> `Diff_field of error ]) result

get_opt v field gets field from v, returning Error (`Diff_field e) on exception.

Sourceval set : 'a -> ('a, 'b) t -> 'b -> 'a

set v field x sets field in v to x, raising Diff_field error on exception

Sourceval set_opt : 'a -> ('a, 'b) t -> 'b -> 'a option

set_opt v field x sets field in v to x, returning None on exception

Sourceval set_res : 'a -> ('a, 'b) t -> 'b -> ('a, [> `Diff_field of error ]) result

set_opt v field x sets field in v to x, returning Error (`Diff_field e) on exception