|
|||||
|
|||||
Related Concepts | |||||
declaration/definition |
declare record |
Syntax
declare record ((extends record-type)? (field field declaration*)) | elsewhere
You can use declare record
to declare a record type.
Here is how you would declare a record type to hold a simple metadata record for a document:
declare record metadata-label field stream author field stream title field stream publisher field integer year field switch in-print initial {true}
Each field in the record is itself of a particular type and is declared just as a local variable of that type would be declared, substituting the word field
for the word local
.
Each field in a record is itself a shelf, meaning that you may declare a field as a multi-value shelf. Thus we can extend our metadata record type to support the possibility that a document may have more than one author:
declare record metadata-label field stream author variable field stream title field stream publisher field stream year field switch in-print initial {true}
A record declaration by itself simply defines a record type. To create a record you must create a variable of the record type.
You can declare an extended record type by including the extends
keyword in the record declaration. An extended record type inherits the fields of its base type:
declare record point field integer x field integer y declare record color field integer r field integer g field integer b declare record pixel extends point field color rgb
Notice the difference between extending a record and declaring a field whose type is another record type. The record type pixel
extends the type point
, thus inheriting its x
and y
fields. It uses the type color
as a type for the field rgb
. It does not acquire individual r
, g
, and b
fields, but a single field named rgb
whose type is color
. A record of type pixel will have three fields, x
, y
, and rgb
.
An type that is an extension may itself be extended to create a still more specialized type. An extended type may not declare field names that match any of the fields in any of the types from which it is extended, since the fields of these names are alredy part of the extended type.
It is possible to declare a record type with no fields. Such a type can act as a base from which a related class of record types can be created by extension.
A record type may be declared elsewhere
.
declare record point elsewhere
The elsewhere declaration does not create a record type. The full record declaration must be given before variables of the record type are declared. However, the elsewhere declaration makes the type name available for use in other record type declarations. This allows you to create recursive record types:
declare record bar elsewhere declare record foo field integer a field bar x variable declare record bar field integer b field foo y
In this example, foo and bar are both record types. Foo has a field of type bar and bar has a field of type foo. This is perfectly legal and potentially useful. Notice however, that at least one of the fields of the mutually referenced types need to be declared variable. If not, each new foo
creates a new bar
and each new bar
creates a new foo
, to infinity. Circular data structures are permitted. However, it is your responsibility to see that they are properly bounded.
You can also create a record type which has a field of is own type. This is particularly useful if you want to create a tree:
declare record node field stream name field node children variable
Related Concepts Records Records, extended Tree data structures |