The PropList structure provides a extensible, but type safe, implementation
of property lists.
Synopsis
structure PropList
Interface
type holder
val newHolder : unit -> holder
val hasProps : holder -> bool
val clearHolder : holder -> unit
val sameHolder : (holder * holder) -> bool
val newProp : (('a -> holder) * ('a -> 'b)) -> {
peekFn : 'a -> 'b option,
getFn : 'a -> 'b,
setFn : ('a * 'b) -> unit,
clrFn : 'a -> unit
}
val newFlag : ('a -> holder) -> {
getFn : 'a -> bool,
setFn : ('a * bool) -> unit
}
Description
type holder-
The type of a property-list container.
val newHolder : unit -> holder-
newHolder ()creates a new property-list holder.
val hasProps : holder -> bool-
hasProps holderreturntrueif, and only if, the holder contains properties (including set flags). val clearHolder : holder -> unit-
clearHolder holderremoves all properties and flags from the holder. val sameHolder : (holder * holder) -> bool-
sameHolder (holder1, holder2)returnstrueif, and only if, the two holders are the same. val newProp : 'a -> holder) * ('a -> 'b -> { … }-
newProp (getHolder, init)creates a new property of type'bassociated with values of type'a, wheregetHolderis a function for getting the holder from a value andinitis a function for defining the initial value of the property for a value. The property is represented by a record of operations, which are as follows:peekFn : 'a -> 'b option-
peekFn objreturnsSOME v, wherevis the value of the property forobj. If the property has not been set forobj, thenNONEis returned. getFn : 'a -> 'b-
getFn objreturns the value of the property forobj. If the property has not been set forobj, then theinitfunction is used to set the initial value of the property. setFn: ('a * 'b) -> unit-
setFn (obj, v)sets the value of the property tovforobj. clrFn : 'a -> unit-
clrFn objremoves the property fromobj.
val newFlag : ('a -> holder) -> { … }-
newFlag getHoldercreates a new boolean property for values of type'a. The property is represented by a record of two functions:getFn : 'a -> bool-
getFn objreturns the value of the flag forobj. setFn : ('a * bool) -> unit-
setFn (obj, b)sets the value of the flag tobforobj.
Flags represent boolean properties in a way that is more space efficient than using
newProp. Basically, atruevalue is represented by the presence of the property in the holder, whilefalseis represented by its absence. This representation affects the behavior ofhasPropsas flags that arefalseare not counted.
Examples
A common use of property lists is to provide a mechanism for attaching attributes to existing types. For example, we might define a representation of variables in a compiler as:
datatype var = V of {
name : string,
props : PropList.holder
}
We might define a use count property as follows:
local
val {getFn, setFn, ...} = PropList.newProp (
fn (V{props, ...}) => props,
fn _ => 0)
in
fun use x = setFn(x, getFn x + 1)
fun countOf x = getFn x
end