[comp.lang.pascal] Reusing identifiers

abcscnuk@csuna.csun.edu (Naoto Kimura) (03/16/89)

In article <134300005@cdp> ipis@cdp.UUCP writes:
>
>I see no reason why it shouldn't be legal.  Are you concerned with the
>multiple declarations of "D" and "T" (no problem because they're inside
>records) or of using a user-defined type in a record?  Either one
>should be fine.
>--andy

There IS a problem.  Suppose we have the following program:

    1	program foo(input,output);
    2	type
    3	    X	= 0..7;
    4	    Y	= record
    5		X : integer;
    6		Z : X
    7		end;
    8	    T	= record
    9		Z : X;
   10		X : integer;
   11		end;
   12	    Z	= X;
   13	begin
   14	end.

at line 3	"X" is defined to be a subrange 0..7
at line 5	Within the scope of the record type "Y", "X" is now a
		field of type integer.   We can reuse the identifier "X"
		because we are not in the same scope as the previous
		definition.
at line 6	We encounter a problem -- "X" isn't a type but is a
		field.
at line 9	We encounter the same problem as we did on line 6
at line 12	We can use "X" as a type, since within the scope of
		the program it is a type.


Believe it or not, the following program really is a legal pascal
program.  I originally wrote it to experiment with scoping of
identifiers.  It should help illustrate the scoping rules.

    1	program pathological (output);
    2	const
    3	   writeln =  1;
    4	   readln  = 50;
    5	type
    6	   real    = writeln..readln;
    7	   write   = (eof, eoln);
    8	   read    = char;
    9	   boolean = packed array [ real ] of read;
   10	var
   11	   get     : read;
   12	   true,
   13	   false   : write;
   14	   dispose : boolean;
   15	
   16	procedure maxint  ( var eoln : text;
   17	                        get  : boolean;
   18	                        eof,
   19	                        read : real     );
   20	   forward;
   21	
   22	procedure integer  ( var eof   : text;
   23	                         get   : boolean;
   24	                         read,
   25	                         write : real    );
   26	   begin
   27	      if read < write then begin
   28	         maxint ( eof, get, write, read+1 );
   29	         eof^ := get[read];
   30	         put (eof);
   31	      end;
   32	   end;
   33	
   34	procedure maxint;
   35	   begin
   36	      if read < eof then begin
   37	         integer ( eoln, get, read, eof-1 );
   38	         eoln^ := get[eof];
   39	         put (eoln);
   40	      end;
   41	   end;
   42	
   43	begin
   44	   true  := eof;
   45	   false := eof;
   46	   dispose := '     .ri rvgitmsso agr iT hspormde oehn eywed     ';
   47	   if true = false then begin
   48	      integer ( output, dispose, writeln, readln );
   49	   end;
   50	end.

                //-n-\\			 Naoto Kimura
        _____---=======---_____		 (abcscnuk@csuna.csun.edu)
    ====____\   /.. ..\   /____====
  //         ---\__O__/---         \\	Enterprise... Surrender or we'll
  \_\                             /_/	send back your *&^$% tribbles !!

bobd@ihf1.UUCP (Bob Dietrich) (03/21/89)

In article <1765@csuna.csun.edu> abcscnuk@csuna.csun.edu (Naoto Kimura) writes:
>Believe it or not, the following program really is a legal pascal
>program.  I originally wrote it to experiment with scoping of
>identifiers.  It should help illustrate the scoping rules.
>
>                //-n-\\			 Naoto Kimura
>        _____---=======---_____		 (abcscnuk@csuna.csun.edu)

While your program is an interesting illustration of the fact that required
identifiers in Pascal are not keywords, it has little to do with scope. BTW,
you might also make use of "forward" as an identifier; many processors
incorrectly make it a keyword (or maybe you did, I may not have read closely
enough).

usenet:	uunet!littlei!intelhf!ihf1!bobd		Bob Dietrich
  or	tektronix!ogccse!omepd!ihf1!bobd	Intel Corp., Hillsboro, Oregon
  or	tektronix!psu-cs!omepd!ihf1!bobd	(503) 696-2092