Contents | Prev | Next | Index
A record type is a structure consisting of a fixed number of elements, called fields, with possibly different types. In a record type declaration, for each field the name and type is specified. The scope of the field identifiers extends from the point of their declaration to the end of the record type. They are also visible within designators refering to fields of record variables.
A record type may have several variant sections, in which case the first field of the section is called the tag field. Its value indicates which variant is assumed by the section. Individual variant structures are identified by case labels. These labels are constants of the type indicated by the tag field.
In the following grammar specification for record types Java specific language extensions for Modula-2 are shown in red color. They include the Java modifier and Java name directives as well as the specification of a parent record type and an interface list.
record_type ::= record field_list_seq END ::= record record_base field_list_seq END record ::= modifier RECORD record_base ::= ( parent interface_list ) parent ::= qualident ::= <empty> interface_list ::= interface_list , qualident ::= <empty> field_list_seq ::= field_list_seq ; field_list ::= field_list field_list ::= modifier field_id_list : type ::= CASE tag OF variant_list else_variant ::= <empty> field_id_list ::= field_id_list , field_name ::= field_name field_name ::= <identifier> ::= <identifier> java_name tag ::= : qualident ::= modifier field_name : qualident variant_list ::= variant_list | variant ::= variant variant ::= variant_label_list : field_list_seq ::= <empty> variant_label_list ::= variant_label_list , variant_labels ::= variant_labels variant_labels ::= const_expr .. const_expr ::= const_expr else_variant ::= ELSE field_list_seq ::= <empty> |
This compiler always maps record types and variants to Java classes. Notice that in this compiler, unlike other Modula-2 implementations, it is not possible to use variants for accessing the same data with different types.
The following object oriented features are similar to those of Oberon-2. They are only available if the Modula-2 language extensions are enabled via a compiler switch or compiler directive.
Record types are extensible. That is, a record type can be declared as an extension of another record type. In the example
T0 = RECORD x : INTEGER END
T1 = RECORD( T0 ) y : REAL END
T1 is a (direct) extension of T0 and T0 is the (direct) base type of T1. The base type is also called a superclass in Java. An extended type T1 consists of the fields of its base type and of the fields that are declared in T1. Identifiers declared must be different from the identifiers declared in its base type(s).
In order to better support the extension of existing Java classes, this Modula-2 compiler also allows the specification of Java interface names as part of the record base. A Java interface is a kind of abstract class which gets implemented by specifying its name in the record base. Its field identifiers must be different from the names of fields in the base type(s) and from the names of fields in other interfaces being implemented. Also, any newly declared field identifier must be different from those in the specified interface(s).
The following are examples of record types:
RECORD
day, month, year : INTEGER;
END
RECORD
name, firstname : ARRAY [0..31] OF CHAR;
age : INTEGER;
salary : REAL
END
Contents | Prev | Next | Index
Canterbury Modula-2 for Java (Last documentation update
Feb 8, 2000)
Copyright © 1998 Mill Hill &
Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to
mhc@webcom.com