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 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 just one kind of object: the string. In object-oriented terminology, each string variable is a object that is an reference to an instance of the String class.

With ooRexx, variables can now reference objects other than strings. In addition to the String class, Rexx includes classes for creating arrays, queues, streams, and many other useful objects. Additionally, you can create your own classes that can interoperate seamlessly with the language built-in classes. Objects in these 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. Conceptually, you are "asking" the String object refered to by variable NAME to give you its substring starting at character 2 for 3 characters. Many String operations are available as both class methods and built-in functions, but the String class also provides many method enhancements for which there are no corresponding built-in functions.

For classes other than String (such as Array or Queue), methods are provided, but not equivalent built-in 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 of the Array class is created and reference to it is stored in the variable MyArray. A period and the class name identify the class, .Array. The period tells the interpreter to look in the Rexx environment for a class named "ARRAY". The Myarray array object has five elements, but the array itself is currently empty. Items can be added using methods defined by the array class.

myarray[1] = "Rick"
myarray[2] = "David"
myarray[3] = "Mark"

say myarray[1] myarray[2] myarray[3]

The Array class implements many more methods. See the ooRexx reference for details on what additional methods are provided.

By adding object technology to its repertoire of traditional programming techniques, Rexx has evolved into an object-oriented language, like Smalltalk. Rexx accommodates the programming techniques of traditional Rexx while adding new ones. With ooRexx, 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.