Product SiteDocumentation 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 (see the STRING method of the Object class STRING Section 5.1.1.24, “string”) and uses this string.
When comparing a string object with the Nil object, if the NOSTRING condition is being trapped, then
if string = .nil
will raise the NOSTRING condition, whereas
if .nil = string
will not as the Nil object's "=" method does not expect a string as an argument.

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. When a method expects a string as an argument, the argument object is sent the REQUEST("STRING") message. If REQUEST returns the Nil object, then the method raises an error.