2.4 RecordsΒΆ

A Record is an object composed of one or more other objects, Record each of which is referenced with a selector. Components can all belong to the same type or each can have a different type.

The syntax for writing a Record type is

Record(selector1:type1, selector2:type2, ..., selectorN:typeN)

You must be careful if a selector has the same name as a variable in the workspace. If this occurs, precede the selector name by a single quote quote.

Record components are implicitly ordered. All the components of a record can be set at once by assigning the record a bracketed tuple of values of the proper length. For example:

r : Record(a:Integer, b: String) := [1, "two"]
\[\mathrm{[a=1,b="two"]}\]

Type: Record(a: Integer,b: String)

To access a component of a record r, write the name r, followed by a period, followed by a selector.

The object returned by this computation is a record with two components: a quotient part and a remainder part.

u := divide(5,2)
\[\mathrm{[quotient=2,remainder=1]}\]

Type: Record(quotient: Integer,remainder: Integer)

This is the quotient part.

u.quotient
\[2\]

Type: PositiveInteger

This is the remainder part.

u.remainder
\[1\]

Type: PositiveInteger

You can use selector expressions on the left-hand side of an assignment to change destructively the components of a record.

u.quotient := 8978
\[8978\]

Type: PositiveInteger

The selected component quotient has the value 8978, which is what is returned by the assignment. Check that the value of u was modified.

u
\[\mathrm{[quotient=8978,remainder=1]}\]

Type: Record(quotient: Integer,remainder: Integer)

Selectors are evaluated. Thus you can use variables that evaluate to selectors instead of the selectors themselves.

s := 'quotient
\[\mathrm{quotient}\]

Type: Variable quotient

Be careful! A selector could have the same name as a variable in the workspace. If this occurs, precede the selector name by a single quote, as in selector:quoting u.’quotient.

divide(5,2).s
\[2\]

Type: PositiveInteger

Here we declare that the value of bd has two components: a string, to be accessed via name, and an integer, to be accessed via birthdayMonth.

bd : Record(name : String, birthdayMonth : Integer)

Type: Void

You must initially set the value of the entire Record at once.

bd := ["Judith", 3]
\[\mathrm{[name="Judith",birthdayMonth=3]}\]

Type: Record(name: String,birthdayMonth: Integer)

Once set, you can change any of the individual components.

bd.name := "Katie"
\[\mathrm{ "Katie"}\]

Type: String

Records may be nested and the selector names can be shared at different levels.

r : Record(a : Record(b: Integer, c: Integer), b: Integer)

Type: Void

The record r has a b selector at two different levels. Here is an initial value for r.

r := [ [1,2], 3 ]
\[\mathrm{[a=[b=1,c=2],b=3]}\]

Type: Record(a: Record(b: Integer,c: Integer),b: Integer)

This extracts the b component from the a component of r.

r.a.b
\[1\]

Type: PositiveInteger

This extracts the b component from r.

r.b
\[3\]

Type: PositiveInteger

You can also use spaces or parentheses to refer to Record components. This is the same as r.a.

r(a)
\[\mathrm{[b=1,c=2]}\]

Type: Record(b: Integer,c: Integer)

This is the same as r.b.

r b
\[3\]

Type: PositiveInteger

This is the same as r.b:=10.

r(b) := 10
\[10\]

Type: PositiveInteger

Look at r to make sure it was modified.

r
\[\mathrm{[a=[b=1,c=2],b=10]}\]

Type: Record(a: Record(b: Integer,c: Integer),b: Integer)