package diff

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

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

type (_, _) 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.

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

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

type 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.

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

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

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

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

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

opt_map f turns f into an optional field.

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

opt_map f monadically binds f into an optional field.

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

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

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

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

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

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

val 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.

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

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

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

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

val 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

OCaml

Innovation. Community. Security.