next up previous contents index
Next: Functions and Constructs Up: Data Previous: Sequences ([])

Record Types (datatype)

   

Record types with a fixed number of slots can be defined with the datatype construct. For example,

defines a record with two slots both which must contain a floating-point number. Defining a record also defines a corresponding function that is used to construct the record. For example,

creates a complex record with 7.1 and 11.9 as its two values.

Elements of a record can be accessed using pattern matching in the let construct. For example,

will remove the real part of the variable a (assuming it is kept in the first slot). More details on pattern matching are given in the next section.

As with functions, records can be parameterized based on type-variables. For example, complex could have been defined as:

    tabular5884

This specifies that for alpha bound to any type in the type-class number (either int or float), both slots must be of type alpha. This will allow either,

    tabular5902

    tabular5920

but will not allow complex(7, ` a) or complex(2, 2.2). The type of a record is specified by the record name followed by the binding of all its type-variables. In this case, the binding of the type-variable is either int or float.



Jonathan Hardwick
Tue Nov 28 13:57:00 EST 1995