| # Examples 6.4: Type Specifiers |

The specification of a type for some entity is done in one of two ways :-

- A class - possibly with one or more instantiated generic arguments - is specified. All of the features of that class - as defined by the implementation - are available for use by the entity while it exists (if a reference attribute or local variable entity then it will need creation by the program code before use).
- A closure - which is either an iter or a routine specified by the types of argument and (if any) returned value. This will only be available for use after some method has been assigned to the closure entity - in a rather similar way to the creation or other initialisation of an entity associated with a class type.

All of the examples below refer to classes specificed by the Required Library for definiteness. This is not meant to imply that only Required Library classes can appear in Sather source text of the kind illustrated here.

### Class examples

Consider the simplest of entity declarations in some class or other -

const Answer : CARD := 42 ;
shared squares : ARRAY{CARD} := | 0,1,4,9,25,36,49,64,81,100 | ;
attr messages : ARRAY{STR} ;

and as local variables in some method the following -

loc_val : BOOL ;
me : SAME ;

in which CARD, ARRAY{CARD}, ARRAY{STR} and BOOL are the type specifiers. Note the use of SAME - this is an extremely useful way of writing 'this type' - of much use in writing partial classes for inclusion in a wide variety of other classes! Any such combinations may be used as needed. In an implementation of one of the Required Library container classes there is, for example,

loc_bucket : DATABUCKET{K,BAG{ELT}} ;

which illustrates that class arguments (K and ELT) can be used in specifying more complex type specifications as may be needed.

### Closure examples

Closures have a particular importance in concurrent programs (for call-back actions) and in explicitly written despatching, dependent dynamically on program input or state - not on type for which the typecase statement should be used. The following should illustrate this kind of type specification -

trig : ROUT{FLT} : ANGLE ;
action : FMAP{CLICK_KIND,ROUT{POINT} : LENGTH} ;

in which trig could be given the value acos, asin or atan from, say, the ANGLE Required Library class. The class CLICK_KIND could be an enumeration of different mouse/state key combinations - for which a different routine needs to be called. This sort of thing could be done in a (large?) case statement, but since all of the actions have an identical signature it is much easier to define the map and initialise it explicitly.

| Specification Index |  | Language Index |  | Section 6 Index

Comments or enquiries should be made to Keith Hopper. Page last modified: Friday, 26 May 2000. |