|
| Previous: 2.9.5 Methods and Messages | TOC | Index | Back | Next: 2.10 Interface Classes |
Constants may be public as well:
PUBLIC CONST symbol = constant_expression;
Such constants can be accessed from outside the class by sending a special form of a message to the class which defines the constant. Given the constant MAXLEN of the class STRING:
PUBLIC CLASS string() PUBLIC CONST MAXLEN = 32767; END
the expression
STRING.MAXLEN
is used to access the value of MAXLEN. So the general form of class constant access is
classname.constname
The 'classic' way of exporting such a constant would be to define a method returning the constant:
PUBLIC maxlen() RETURN 32767;
Class constants have the advantage of saving a procedure call. Since their values are known at compile time, they can also be used in constant expression contexts.
Structures can be exported in the same way as constants:
PUBLIC STRUCT structname = member1, ..., memberN;
Public structures are useful for describing structured arguments or return values of methods. For example, the SYSTEM.STAT function (the function STAT of the SYSTEM class) returns a structure containing information about a specific file. In addition, the SYSTEM class provides a public structure describing the layout of the structure delivered by the STAT function. This structure allows programs using the SYSTEM.STAT function to decompose the returned information.
Since class constants cannot be altered, they do not weaken the strict encapsulation principle. Public structures are an interface and an implementation at the same time.
| Previous: 2.9.5 Methods and Messages | TOC | Index | Back | Next: 2.10 Interface Classes |