INTERFACE usage
Defining interfaces
An interface is defined by a group of methods that apply on user-defined types, to define a common usage interface for several individual types.
All elements inside an INTERFACE
must be and can only be methods for a user-defined type.
The next code example defines a variable as an interface:
DEFINE v INTERFACE
kind () RETURNS STRING,
area () RETURNS FLOAT
END INTERFACE
TYPE
declaration
to reuse an interface
definition:TYPE Shape INTERFACE
kind () RETURNS STRING,
area () RETURNS FLOAT
END INTERFACE
A variable defined with an interface can receive any type related to the interface.
Associating types to an interface
A user-defined type for which methods are defined is implicitly associated to any interface that defines a set of methods for this type.
Rectangle
gets associated to a method named
area()
:TYPE Rectangle RECORD
height, width FLOAT
END RECORD
FUNCTION (r Rectangle) area () RETURNS FLOAT
RETURN r.height * r.width
END FUNCTION
area()
method becomes implicitly an interface for
the type
Rectangle
:TYPE Shape INTERFACE
area() RETURNS FLOAT
END INTERFACE
v
with an INTERFACE
structure listing
the area()
method, you can assign a variable defined as Rectangle
to v
, and invoke the method with
v.area()
:FUNCTION main()
DEFINE r Rectangle = ( height:10, width:20 )
DEFINE v Shape
LET v = r
DISPLAY v.area()
END FUNCTION
Implementation tips
For maximum flexibility, consider implementing the types and corresponding methods in individual modules, and implement the interface in another individual module.
In the parent module using the types/methods and interfaces, import each module with the IMPORT FGL
instruction.