Passing a struct to a subprogram or function
When calling a subprogram or function with a STRUCT argument, you can
pass an object either anonymously or specify a type name.
CALL Sub(STRUCT A)
...
SUB Sub(A {AS|:} TypeName)
SUB Sub(STRUCT A)
Like regular variables, types can either be passed by value or by reference.
When passed by value, a copy is passed to the subroutine.
For example:
TYPE Type
INTEGER I
END TYPE
!
DIM Inst:Type
Inst.I=0
CALL Sub(Inst)
PRINT Inst.I
CALL Sub((Inst))
PRINT Inst.I
STOP
!
SUB Sub(STRUCT A)
A.I=A.I+1
SUBEND
Instances can be passed to a SUBroutine either anonymous or with an
associated type.
- When a type is specified and it is defined globally, the passed
variable must either be of the same type or it must have a common
base type.
- Then the type specified is not defined globally, it is defined
with the given name from the parent environment.
For example:
! Define type Tsample
TYPE Tsample
INTEGER I
DIM X$[20]
END TYPE
! DIM variable A with type Tsample
DIM A AS Tsample
! Pass A to SUB
CALL Sub1(STRUCT A)
CALL Sub2(STRUCT A)
STOP
! Sub1 requires a type Tsample (or any derived type)
SUB Sub1(A AS Tsample)
PRINT A.I;A.X$
SUBEND
! Sub2 does accept an anoymous variable
SUB Sub2(STRUCT A)
PRINT A.I;A.X$
SUBEND
Runtime type identification
The TYPEOF$ function can be used to identify an instance.
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
Data base integration
The user defined type concept is designed to operate with the
Eloquence data base. The IN DATA SET ... DEFINE TYPE
statement can be used to define data types from the data base schema
at runtime, the PACKFMT, IN DATA SET LIST and
IN DATA SET ... USE have been enhanced to support user
defined data types.
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$)
...