Using Objects in REXX Clauses

To extend our exploration of objects, let's begin with the Myarray object. These examples with Myarray illustrate how to use new objects in REXX clauses. In the expression:

myarray=.array~new(5)

a new instance of the Array class, Myarray, is created. (Recall that a period precedes a class name in an expression, to distinguish the class from other variables.) Our Myarray array object has five elements.

After Myarray is created, you'll want to assign values to it. One way is with the PUT method. PUT has two arguments, which must be enclosed in parentheses. The first argument is the string to be placed in the element. The second is the number of the element in which to place the string. Here, we associate the string object "Hello" with the third element of Myarray:

myarray~put("Hello",3)

REXX dynamically adjusts the size of the array to accommodate the new element.

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 third element of Myarray:

say myarray~at(3)

Results:

Hello

The SAY instruction expects the string object as input, and that is precisely what AT returns. So what happens if you put a non-string object in the SAY instruction? SAY sends a STRING message to the object. The STRING method for REXX's built-in objects returns a human-readable string representation for the object. In our example, the STRING method for Myarray returns the string "an array":

say myarray  /* SAY sends STRING message to Myarray */

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)

Results:
HelloHelloHelloHello

This example gets the same result using only methods:

say myarray~at(3)~copies(4)

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 have been made to improve the reliability of the language. This example uses the []= method (left-bracket right-bracket equal-sign) and [] method to set and retrieve array elements:

myarray[4]="the fourth element"
say myarray[4]

Although the above two statements look like an ordinary array assignment and array reference, they are really messages to the Myarray object. You can prove it to yourself by executing these equivalent statements, which use the twiddle to send the messages.

myarray~"[]="("a new test",4)
say myarray~"[]"(4)

Similarly, expression operators (such as +,-,/, and *) are actually methods. But you don't have to use the twiddle to send them:

say 2+3      /* Displays 5 */
say 2~'+'(3) /* Displays 5 */

In the second SAY statement, quotes are needed around the + message because it is a character not allowed in a REXX symbol.

Now let's move on to methods. There are three in particular that you'll use often.


[Back: A Closer Look at Objects]
[Next: Common Methods You'll Want to Define]