NEXT ·  UP ·  PREVIOUS ·  CONTENTS ·  INDEX

Pairs and record types

We have used pairs and tuples without stating their type. A pair with an integer as the left element and a boolean as the right has type ``int * bool''. Note that int * bool * real is neither (int * bool) * real nor int * (bool * real). Pairs and tuples are themselves simply records with numbered fields. The label for a field can also be a name such as age. Each field has an associated projection function which retrieves the corresponding value. The projection function for the age field is called #age. The following record value has type { initial : char, surname : string, age : int }.

val author = { initial = #"S", surname = "Gilmore", age = 36 };

Then #surname (author) is "Gilmore", as expected.

This has shown us another of the derived forms of the language. The pair and tuple notation is a derived form for the use of record notation. Thus the meaning of the second of the declarations below is the first.

val n : { 1 : int, 2 : bool } = { 1 = 13, 2 = false }

val n : int * bool = (13, false)

NEXT ·  UP ·  PREVIOUS ·  CONTENTS ·  INDEX