]>
Book Contents Previous Section 2.3 Declarations Next Section 2.5 Unions Book Index

## 2.4 Records

A Record is an object composed of one or more other objects, Record each of which is referenced selector:record with record:selector 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(selector 1:type 1, selector 2:type 2, ..., selector N:type N)

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"]

[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)

[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

[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

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]

[name="Judith",birthdayMonth=3]

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

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

bd.name := "Katie"

"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 ]

[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)

[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

[a=[b=1,c=2],b=10]

Type: Record(a: Record(b: Integer,c: Integer),b: Integer)
Book Contents Previous Section 2.3 Declarations Next Section 2.5 Unions Book Index