[net.micro] Arrays in Apple PASCAL Units?

muller@inmet.UUCP (03/23/84)

#N:inmet:5800047:000:1291
inmet!muller    Mar 22 01:14:00 1984

Question:  In Apple PASCAL 1.1, how can one write a procedure to be included
in a Units library file that will take variable-dimensioned arrays passed to
it as referenced parameters?  If you give the procedure statement in the 
Interface part of the Unit and specify an array type (to insure that the
types will agree between the procedure and any calling program) the compiler
doesn't like it.  Apparantly you must either use a standard type or declare
the type in the Interface section, which produces another compiler error if
the type statement comes before the var statement.  This precludes any non-
constant subscript definitions for a utility procedure, as near as I can see.
  A related question is:  Can you declare variables local to a procedure in
a Unit in the procedure block, or must the all go in the interface section?
If you cannot, then all procedures and functions in any Unit must see the
same variable identifiers.
 Finally, what does Apple PASCAL need for two arrays to be of the same type?
Just similar element types and subscript ranges, or is the requirement more
strict?  These two questions relate to how one might write "generalized"
procedures in a Unit the can take arrays as parameters.
   Thanx,   Jim Muller     ima!inmet!muller    or    harpo!inmet!muller

allyn@sdcsvax.UUCP (03/23/84)

It is extremely dirty to use variably dimensioned arrays AT ALL in Pascal.
The only way I know of is to declare a pointer to your array type declared
like this: array[0..0] of whatever;  If you turn rangechecking off (*$R-*)
then you can access all the memory you want.  In this case, you should
then pass the pointer and the dimension to the unit procedure.  Be careful
of stack overflows.

You may declare variables, procedures, and functions local to the
implementation section of a unit.

I believe that variables must be of the *exact same* type to be equivalent.

If you need more help, mail me.
-- 
 From the virtual mind of Allyn Fratkin
                          UCSD Pascal Project
                          U.C. San Diego

ags@pucc-i (Seaman) (03/26/84)

The March, 1984 BYTE has an article on "Pascal's Design Flaws:  Modula-2
Solutions and Pascal Patches" by Mark C. Johnson and Allen Munro.  The
article lists "Pascal's Seven Deadly Sins" and demonstrates how each
of these problems is solved in Modula-2, and how Pascal programmers
can work around the problems.

One of the seven flaws in Pascal is, of course, the lack of open array
parameters.  In Modula-2, one can declare

		PROCEDURE Foo(VAR A : ARRAY OF Bar);

and have Foo compatible with all arrays of Bar, regardless of size or
index type.  As a special case, the declaration

		PROCEDURE Baz(VAR B : ARRAY OF WORD);

is compatible with any Modula-2 type (the actual parameter doesn't even
have to be an array type).

If you don't have Modula-2 on your Apple, there are at least two ways
to work around the problem.  One method, described in the article and
already mentioned on the net, involves a type "array[0..0] of bar",
disabled range checking and some games with variant records.

Another method is available in Apple Pascal if you are willing to write
the library routine in assembly language.  Just declare it

		procedure Baz(var B); external;

and it will be compatible with any variable (regardless of type), just
like the Modula-2 declaration above.  Unlike the Modula-2 procedure, your
assembly language routine won't be able to find out how large the actual 
parameter is unless you pass that information as another parameter.
-- 

Dave Seaman
..!pur-ee!pucc-i:ags

"Against people who give vent to their loquacity 
by extraneous bombastic circumlocution."