Object Pascal
Volume Number: 2
Issue Number: 12
Column Tag: Pascal Procedures
Introduction to Object Pascal
By Ken Doyle, Apple Computer, Inc.
Introduction
If you have been reading about MacApp, you may be wondering if you have to have
the Macintosh Programmer's Workshop (MPW) and if MacApp programs must be
written in Object Pascal. The answer is yes, for now. For those of you who like some of
the other Pascal compilers out there, or prefer to program in another language such as
C, you are currently out of luck. The reason is two-fold. Most languages for the
Macintosh do not support the object-oriented concepts upon which MacApp so heavily
relies, and even if they do, they don't use the same run-time scheme that Object Pascal
does.
In this article I will first present a description of the syntax of Object Pascal and
comment on some of the semantics involved in using the syntax. I will discuss the
various degrees of compatibility that another language or compiler needs to achieve in
order to make use of MacApp and what steps are necessary in meeting that goal. In
particular, the exact format of the generated code and the run-time routines that deal
with that code will be shown. I will talk a bit about how we added objects to MPW's
Assembly language. Finally I present a scheme for optimizing what we've already
learned.
Object Pascal
Object Pascal is an extension to the Pascal language that was developed at Apple
in consultation with Niklaus Wirth, the inventor of Pascal. It is descended from an
earlier attempt at an object-oriented version of Pascal called Clascal, which was
available on the Lisa computer. MacApp itself is descended from the Lisa Toolkit, an
application framework for creating Lisa applications. The Lisa Toolkit was written in
Clascal.
There are actually very few syntactic additions to Pascal in Object Pascal. A new
data type is added, the object. An object is very much like a record in that it can have
multiple data fields of arbitrary types. In addition, you can specify a list of procedures
and functions, referred to as methods, for a particular object type. These methods
define the actions that an object of this type can perform. For example, you could
define a Shape object type as follows:
type
Shape = object
bounds: Rect;
color: Pattern;
procedure Draw;
procedure Erase;
procedure Rotate(angle: integer);
procedure Move(delta: Point);
function Area: integer;
end;
Furthermore, you can define an object type that inherits the fields and methods
of another object type. The new type can define additional fields and methods and can
choose to selectively override methods that it has inherited.
type
Circle = object(Shape)
radius: integer;
procedure Draw; override;
function Area: integer; override;
procedure SetRadius(newRadius: integer);
end;
var aCircle: Circle;
An object type is often referred to as a class. In the above example, Circle is a
subclass of Shape. Shape is the superclass of Circle. A class (object type) can have
many subclasses (descendants), but only one superclass (immediate ancestor). When
speaking of the relationships conceptually , I will more often use the class terminology.
When speaking in terms of Pascal data types I use the object type terms.
Objects are created by calling the Pascal built-in procedure New on a variable of
an object type. You say New(aCircle) to create an instance of the object type Circle.
The New procedure, when used with an object type variable allocates sufficient storage
on the heap for the object and sets the value of the variable to be a handle (pointer to a
pointer) to that data. The double arrow normally required for handle dereferencing is
done automatically by the compiler, so fields are accessed directly, eg: aCircle.bounds,
NOT aCircle^^.bounds. Likewise, to invoke a method you use the same notation:
aCircle.Draw invokes the Draw method of the Circle object type, presumably drawing
itself on some display. Since all object type variables are actually handles to the data,
an assignment such as shape1 := shape2 causes shape1 to point to the same data as
shape2.
The fields of an object can themselves be references to other objects. For