A Classic Language Gets Classier

Object-oriented extensions have been added to traditional Rexx without changing its existing functions and instructions. So you can continue to use Rexx's procedural instructions, and incorporate objects as you become more comfortable with the technology. In general, your current Rexx programs will work without change. In contrast to the traditional Rexx interpreter, the Object Rexx interpreter checks all the syntax at start-up time, and this means that any syntax errors that exist will be found.

In object-oriented technology, objects are used in programs to model the real world. Similar objects are grouped into classes, and the classes themselves are arranged in hierarchies.

As an object-oriented programmer, you solve problems by identifying and classifying objects related to the problem. Then you determine what actions or behaviors are required of those objects. Finally, you write the instructions to generate the classes, create the objects, and implement the actions. Your main program consists of instructions that send messages to objects.

A billing application, for example, might have an Invoice class and a Receipt class. These two classes might be members of a Forms class. Individual invoices are instances of the Invoice class.

Figure 2-1. Objects in a Billing Application

Each instance contains all the data associated with it (such as customer name or descriptions and prices of items purchased). To get at the data, you write instructions that send messages to the objects. These messages activate coded actions called methods. For an invoice object, you might need CREATE, DISPLAY, PRINT, UPDATE, and ERASE methods.

From Traditional Rexx to Object Rexx

In traditional (classic) Rexx, all data was stored as strings. The strings represented character data as well as numeric data. From an object-oriented perspective, traditional Rexx had one kind of objects: strings. In object-oriented terminology, each string variable is an object that is an instance of the String class.

In Object Rexx, variables can now reference objects other than strings. In addition to a string class, Rexx now includes classes for creating arrays, queues, streams, and many other useful objects. Objects in these new Rexx classes are manipulated by methods instead of traditional functions. To activate a method, you just send a message to the object.

For example, instead of using the SUBSTR function on a string variable Name, you send a SUBSTR message to the string object. In classic Rexx, you would do the following:

s=substr(name,2,3)

In Object Rexx, the equivalent would be:

s=name~substr(2,3)

The tilde (~) character is the Rexx message send operator, called twiddle. The object receiving the message is to the left of the twiddle. The message sent is to the right. In this example, the Name object is sent the SUBSTR message. The numbers in parentheses (2,3) are arguments sent as part of the message. The SUBSTR method is run for the Name object, and the result is assigned to the s string object.

For the new classes (such as array or queue), methods are provided, but not equivalent functions. For example, suppose you want to use a Rexx array object instead of the traditional string-based stem variables (such as text.1 or text.2). To create an array object of five elements, you would send a NEW message to the array class as follows:

myarray=.array~new(5)

A new instance, named Myarray, of the Array class is created. A period and the class name identify the class. The period is necessary because it precedes the class name in an expression. The Myarray array object has five elements. Some of the other methods, besides NEW, for array objects are PUT, AT, REMOVE, SIZE, [], and []=.

By adding object technology to its repertoire of traditional programming techniques, Rexx has evolved into an object-oriented language, like Smalltalk. Object Rexx accommodates the programming techniques of traditional Rexx while adding new ones. With Object Rexx, you can use the new technology as much or as little as you like, at whatever pace you like. You can mix classic and object techniques. You can ease into the object world gradually, building on the Rexx skills and knowledge you already have.