4 Data Variables and Data handling
Syntax:
TYPE type_name [EXTENDS base_type_name]
.
.
.
END TYPE
They are different to the string and numeric variable types known in Eloquence, because they have to be defined before they can be used. The standard types are internally defined. The type definition can be compared with a design, as example for a vehicle. With the type definition, it is defined on which parts the vehicle consists. This vehicle has an engine_type, a number of wheels, a colour and so on.
Example:
TYPE Tvehicle DIM Engine$[1] INTEGER Wheels DIM Colour$[20] . . . END TYPEIn a TYPE constructions only DIM, INTEGER, DINTEGER, SHORT, REAL and Comment lines are allowed.
TYPE Tname [EXTENDS Tbase]
EXPORT TYPE Tname [EXTENDS Tbase]
IN DATA SET ... DEFINE TYPE Tname [EXTENDS Tbase]
IN DATA SET ... EXPORT TYPE Tname [EXTENDS Tbase]
By default a type definition is local to the current segment. This is also true for types defined in the main segment. Therefore, types used in COM statements must be "exported" in order to be usable in a subprogram. Otherwise a runtime error message will be issued.
NOTE: This is a behaviour change.Initially, all types defined in the main segement were global.
When a segment defines and EXPORTs a type with a name which has been exported from a higher level, the newly exported type permanently replaces the former type for all subsequently called lower level segments. If the type which is most recently defined is not EXPORTed, the previously exported type again becomes available to lower called segments.
In the above example derived types as car, track or motorcycle from the Base-Type Tvehicle are possible. All of them have inherited the members of Tvehicle and some individuell members.
Example:
TYPE Car EXTENDS Tvehicle DIM Engine_type$[30] INTEGER Power, Persons END TYPEA new type can be derived from car again and all members are inherited, the one from the Base-Type Vehicle and the members from car.
Please note, that a base type can be defined after a derived type. It must be defined however before a derived type is instantiated.
Nested types are not supported, this means that it is not possible to define a user defined type as a member of a user defined type.
DIM and COM statements:
COM Instance_name:Type_name COM Instance_name AS Type_name DIM Instance_name:Type_name DIM Instance_name AS Type_nameThe NEW statement:
NEW Instance_name:Type_name NEW Instance_name AS Type_nameThe Instance_name is the variable name and the Type_name is the name of the data type. The instance name and data type name are either separated by a colon or the keyword AS.
Example
DIM Vehicle AS Tvehicle or DIM Vehicle:TvehicleIn addition, the NEW STRUCT statement can be used to create a new, identical copy of the referenced object.
NEW STRUCT Instance_name=Instance_Name
The example below creates a new object named Clone. It will be an exact copy of the referenced object Entry.
DIM Entry:Tphone
Entry.Name$="Joe Sample"
Entry.Phone$="(202) 243 1440"
NEW STRUCT Clone=Entry
Vehicle.colour$="red"
PRINT Vehicle.colour$
In addition to accessing single variables, you can specify the whole object at once. The example below prints all member variables of Entry.
PRINT STRUCT Vehicle
The STRUCT statement can also be used to copy the value of an object:
STRUCT A=B
When copying an object to another, both must be compatible.
a single member variable. This is similar to the Array(*) notation in Eloquence which causes
an operation on the whole array instead of a single element.
The following statements can be used with STRUCT:
X$=TYPEOF$(instance_name)
This returns the type name of the given instance.
The IS A operator can be used to categorize an instance.
IF instance_name IS A type_name THEN ...
If the instance is either of the specified type or derived from it, the IS A operator returns nonzero.
For example:
TYPE Tbase INTEGER A END TYPE TYPE Tderived EXTENDS Tbase INTEGER Q END TYPE ! DIM Derived:Tderived,Base:Tbase DISP "Base is of type ";TYPEOF$(Base) DISP "Inst is of type ";TYPEOF$(Derived) DISP "Base is a Tbase =";Base IS A Tbase DISP "Base is a Tderived =";Base IS A Tderived DISP "Derived is a Tbase =";Derived IS A Tbase DISP "Derived is a Tderived =";Derived IS A Tderived STOP
For example:
DBOPEN(Db$,"",1,S(*)) ... IN DATA SET "CUSTOMER" DEFINE TYPE Tcust NEW Cust:Tcust IN DATA SET "CUSTOMER" USE STRUCT Cust ... DBGET(Db$,"CUSTOMER",7,S(*),"@",Buf$,Key$) ...Of course, types can also be defined statically in your program:
TYPE Tcust
DIM No$[6]
DIM Name$[30]
...
END TYPE
DIM Cust:Tcust
!
DBOPEN(Db$,"",1,S(*))
...
IN DATA SET "CUSTOMER" USE STRUCT Cust
...
DBGET(Db$,"CUSTOMER",7,S(*),"@",Buf$,Key$)
...
ERRN Description
13 Array dimensions not specified or undefined type
900 Undefined base type
901 Nested types are not supported
902 Statement not allowed in type definition
903 Illegal or incomplete type definition
905 No such member variable.
This runtime error occurs, whenever a specified member variable
cannot be found.
! common block TYPE Tglobal INTEGER Iv DIM Xv$[18] INTEGER A(1:2) END TYPE ! COM Global:Tglobal READ STRUCT Global DATA 123,"COMMON",1,2 ! PRINT "Global.Iv=";Global.Iv PRINT "Global.Xv$=";Global.Xv$ PRINT "Global.A(*)=";Global.A(1);Global.A(2) CALL Sub STOP ! SUB Sub COM Global:Tglobal PRINT "Global.Iv=";Global.Iv PRINT "Global.Xv$=";Global.Xv$ PRINT "Global.A(*)=";Global.A(1);Global.A(2) SUBEND