Product Site

4.2.11. Required String Values

Rexx requires a string value in a number of contexts within instructions and built-in function calls.

If you supply an object other than a string in these contexts, by default the language processor converts it to some string representation and uses this. However, the programmer can cause the language processor to raise the NOSTRING condition when the supplied object does not have an equivalent string value.

To obtain a string value, the language processor sends a request("STRING") message to the object. Strings and other objects that have string values return the appropriate string value for Rexx to use. (This happens automatically for strings and for subclasses of the String class because they inherit a suitable makeString method from the String class.) For this mechanism to work correctly, you must provide a makeString method for any other objects with string values.

For other objects without string values (that is, without a makeString method), the action taken depends on the setting of the NOSTRING condition trap. If the NOSTRING condition is being trapped (see Chapter 11, Conditions and Condition Traps), the language processor raises the NOSTRING condition. If the NOSTRING condition is not being trapped, the language processor sends a string message to the object to obtain its readable string representation and uses this string.
Example 4.14. Comparing to the .nil object
d = .directory~new
say substr(d,5,7)         /* Produces "rectory" from "a Directory" */
signal on nostring
say substr(d,5,7)         /* Raises the NOSTRING condition */
say substr(d~string,3,6)  /* Displays "Direct" */

For arguments to Rexx object methods, different rules apply.
For String arithmetic, comparison, and concatenation methods:

These methods always require a string argument, so first a request("STRING") message is sent to the argument object. If request returns .nil because the argument object does not have a makeString method, and the NOSTRING condition is not being trapped, a string message is sent to the object to obtain its string representation.
For all other methods:

When a method expects a string as an argument, the argument object is sent the request("STRING") message. If request returns .nil, the method raises an error.