The PropList structure provides a type-safe implementation of property lists (also known as association lists). The original implementation was provided by Stephen Weeks.

The module provides a holder type that is a mutable container for key/value pairs. The binding

val {peekFn, getFn, setFn, clrFn} = PropList.newProp (selHolder, init)

defines a new property and returns a record of operations to manipulate it. The argument selHolder is a function that is used to select the holder field from an object and the argument init is a function that is used to create the initial property value (if getFn is called before setFn). The difference between the peekFn and getFn operations is that peekFn returns NONE when the property has not yet been created, whereas getFn will allocate and initialize the property (using init). The setFn operation can either be used to initialize an undefined property or to override a property’s current value. While properties can be updated using the setFn, it is usually more efficient to represent a frequently changing property as a reference cell.

structure PropList : PROP_LIST

signature PROP_LIST =
  sig

    type holder
        (* a holder is a mutable container for key/value pairs *)

    val newHolder : unit -> holder
        (* create a new holder *)

    val hasProps : holder -> bool
        (* return true if the holder has any properties. *)

    val clearHolder : holder -> unit
        (* remove all properties and flags from the holder *)

    val sameHolder : (holder * holder) -> bool
        (* returns true, if two holders are the same *)

    val newProp : (('a -> holder) * ('a -> 'b)) -> {
            peekFn : 'a -> 'b option,
            getFn  : 'a -> 'b,
            setFn  : ('a * 'b) -> unit,
            clrFn  : 'a -> unit
          }
        (* creates a new property for objects of type 'a and returns
         * functions to get the property, set it, and clear it.
         *)

    val newFlag : ('a -> holder) -> {
            getFn : 'a -> bool,
            setFn : ('a * bool) -> unit
          }
        (* creates a new boolean property for objects of type 'a and returns
         * functions to get and set the property.
         *)

  end (* signature PROP_LIST *)