Using Objects in Rexx

The following examples with myarray illustrate how to use new objects in Rexx programs.

myarray=.array~new(5) /* array with initial capacity of 5 items */

creates a new instance of the Array class, and assigns to the variable myarray. The period precedes a class name in an expression, to distinguish the class environment symbol from other variables. The myarray array object has an initial capacity for five items.

After the array is created, you can assign values (items) to it. One way is with the PUT method. The PUT has two arguments, which must be enclosed in parentheses. The first argument is the value (item) added to the array, the second is the index number, the number of the location at which to store the value (item) in the array object. Here, the string object Hello is stored at the index numbered 3 of myarray:

myarray~put("Hello",3) /* storing "Hello" at position 3 */

One way to retrieve values from an array object is by sending it an AT message. In the next example, the SAY instruction displays the value (item) stored at position (index) 3 of myarray:

say myarray~at(3) /* fetching item from position 3 */
Results:
Hello

The SAY instruction expects a string object as input, which is what AT returns in this case. If you try to display a non-string object in the SAY instruction, SAY requests a string object, which will cause the MAKESTRING message to be sent to the object if present, and if not the STRING message instead (the exact procedure is documented in the Open Object Rexx: Reference , section Required String Values and applies to any instruction or built-in function that requires a string value). In this example, the MAKESTRING method for an Array object returns the items currently stored in the array, in this case the string Hello.

say myarray /* SAY causes the MAKESTRING message to be sent to myarray */
Results:
Hello

By contrast the STRING method of the Array class returns the string " an Array":

say myarray~string /* sending explicitly the STRING message */
Results:
an Array

Whenever a method returns a string, you can use it within expressions that require a string. Here, the element of the array that AT returns is a string, so you can put an expression containing the AT method inside a string function like COPIES():

say copies(myarray~at(3),4) /* fetching item from position 3, copying it 4 times */
Results:
HelloHelloHelloHello

This example produces the same result using only messages that cause the appropriate methods to be run:

say myarray~at(3)~copies(4) /* fetching item from position 3, copying it 4 times */
Results:
HelloHelloHelloHello

Notice that the expression is evaluated from left to right. You can also use parentheses to enforce an order of evaluation.

Almost all messages are sent using the twiddle, but there are exceptions. The exceptions are to improve the readability of the language. The following example uses the []= (left-bracket right-bracket equal-sign) and [] methods to set and retrieve array elements:

myarray[4]="the fourth element" /* storing string at position 4 */
say myarray[4] /* fetching item (a string) from position 4 */
Results:
the fourth element

Although the previous instructions look like an ordinary array assignment and array reference, they are actually messages to the Array object referenced by myarray. You can prove this by executing these equivalent instructions, which use the twiddle to send the messages:

myarray~"[]="("a new test",4) /* storing string at position 4 */
say myarray~"[]"(4) /* fetching item (a string) from position 4 */
Results:
a new test

Similarly, expression operators (such as +, -, /, and *) are actually methods, but you do not have to use the twiddle to send them:

say 2+3 /* adds 3 to 2, displays result: 5 */
say 2~"+"(3) /* message version: adds 3 to 2, displays result: 5 */

In the second SAY instruction, "+" must be a literal string because the message name contains characters not allowed in a Rexx symbol.