Subsections

Abbreviated declarations

You have now met many identity declarations. When declaring names, it is apparent that much of the declaration is repeated on both sides. For example:

   REF[]REAL r = LOC[10]REAL

Declarations of names are very common in Algol 68 programs and abbreviated declarations are available. The above declaration can be written

   LOC[10]REAL r

or, most commonly

   [10]REAL r

An abbreviated declaration uses the actual-declarer (the right-hand side of an identity declaration) followed by the identifier; and if the actual-declarer contains the generator LOC, you can omit the LOC (see section 6.1 which explains actual-declarers and formal-declarers).

Here are some of the declarations given as examples in this chapter rewritten in their abbreviated form:

   INT a;
   REAL x:=pi;
   CHAR s;
   [7]INT i7;
   [0:6]INT i7 at 0;
   [3]INT k:=(1,2,3);
   [3,3]REAL x; [0:2,0:2]REAL y;
   FLEX[1:0]INT fn;
   [36]CHAR sf;
   [(INT i; read(i); i)]INT r

It is important to note that identity declarations should not be mixed with abbreviated name declarations because the modes are quite different. For example, in

   REAL a:=2.4;
   REAL b = a+2.1

the mode of a is REF REAL, but the mode of b is REAL. In the abbreviated declaration of a name, the mode given is that of the value to which the name will refer (the actual-declarer).

When you declare a new object, if you do not intend assigning to it, use an identity declaration. Only declare it as a name if you intend superseding the value to which it will refer. Remember that assignment can be dangerous because values are superseded.


Exercises

5.19
Write abbreviated declarations for the following:
Ans[*]
(a)
REF[]CHAR rc = LOC[1000]CHAR

(b)
REF FLEX[]INT fi = LOC FLEX[1:0]INT

(c)
REF BOOL b = LOC BOOL := TRUE

5.20
Write full identity declarations for the following:
Ans[*]
(a)
INT a,b,c

(b)
REAL x;[5]CHAR y;[3,3]REAL z

(c)
FLEX[1:0]CHAR s


Sian Mountbatten 2012-01-19