«previous next»

Object-Oriented Programming

SwiftForth includes a powerful object-oriented programming package called SWOOP. It includes all the essential features of object-oriented programming, including:

SwiftForth's support of object-oriented programming is extensive and powerful. Details are, of course, presented in the product manual, from which the following brief introductory information is adapted.

POINT (defined below) is a simple class we shall use as a primary building-block example for SWOOP. It demonstrates two of the four basic class member types: data and colon. The word following CLASS is the name of the class; all definitions between CLASS and END-CLASS belong to it. These definitions are referred to as the members of the class. When a class name is executed, it leaves its handle (xt) on the stack. The constructor words are the primary consumers of this handle.

 
    CLASS POINT
	    VARIABLE X
	    VARIABLE Y
	    : SHOW ( -- )   X @ . Y @ . ;
	    : DOT ( -- )   ." Point at " SHOW ;
    END-CLASS

The class definition itself does not allocate any instance storage; it only records how much storage is required for each instance of the class. VARIABLE reserves a cell of space and associates it with a member name.

The colon members SHOW and DOT are like normal Forth colon definitions, but are only valid in the execution context of an object of type POINT. X and Y also behave exactly like normal Forth VARIABLEs.

There are five kinds of members:

Objects may be static or dynamic. Both early and late binding are supported fully. Instance data and methods are implemented in a manner that is extremely efficient in the context of a Forth development environment.

«previous next»