A Sample Program Using Directives

Here is a program that uses directives to create some new classes and methods:

asav = .savings~new              /* executable code begins */
say asav~type                    /* executable code        */
asav~name= 'John Smith'          /* executable code ends   */

::class Account                  /* directives begin ...   */

  ::method 'TYPE'
    return "an account"

  ::method 'NAME='
    expose name
    use arg name

::class Savings subclass Account

  ::method 'TYPE'
    return "a savings account"   /* ... directives end     */

The preceding program uses the ::CLASS directive to create two classes, the Account class and its Savings subclass. In the ::class Account expression, the ::CLASS directive precedes name of the new class, Account.

The example program also uses the ::METHOD directive to create TYPE and NAME= methods for Account. In the ::method 'TYPE' expression, the ::METHOD directive precedes the method name, and is immediately followed by the code for the method. Methods for any new class follow its ::CLASS directive in the program, and precede the next ::CLASS directive.

In the ::method 'NAME=' method, the USE ARG instruction retrieves the argument. The EXPOSE instruction (which must immediately follow the ::METHOD directive) makes the value (here, "John Smith") available for use by other methods. A variable on an EXPOSE instruction is called an object variable.

You don't have to do anything (like define an object handle) to associate object variables with a specific object. REXX keeps track of object variables for you. Whenever you send a message to savings account Asav, which points to the Name object, REXX knows what internal object value to use. If you assign another value to Asav (such as "Mary Smith"), REXX will delete the object that was associated with Asav ("John Smith") as part of its normal garbage-collection operations.

In the Savings subclass, a second TYPE method is created that supersedes the TYPE method Savings would otherwise have inherited from Account. Note that the directives appear after all the other program code.


[Back: How Directives Are Processed]
[Next: Another Sample Program]