Syntax of RPL methods

Category |  Purpose |  Intended Audience |  Applicable to |  Description |  Limitations |  See also


Category:

Top

RPL


Applicable to:

Top

All Versions


Purpose:

Top

Discuss syntax of RPL methods


Intended Audience:

Top

Users of RPL


Description:

Top

The generic syntax for invoking a method on an object is:

    <object> <method>

where <method> is the operation to be performed by the <object> (the methods available are class dependent). For example:

msg data

invokes the data method on the msg object, which returns a data object. The term data is overloaded (i.e., the same name is used for both the data method on the msg object and the data object itself).

Because each invocation returns an object, invocations can be easily concatenated. For example, if the data element of a message contains a field fieldA, then:

(msg data) fieldA

would invoke the fieldA method on the data object returning a fieldA object. The parentheses are redundant but are included here for clarity. In cases where it is necessary to pass a parameter to a method the generic syntax is:

    <object> <method>: <parameter>

where <parameter> is an object and a colon (:) is used to indicate that a parameter follows the method. For example:

collection add: element

invokes the add: method on the collection object passing in the element object as a parameter.

Similarly, a method may require two (or more) parameters, for example the expression:

array at: index put: element

invokes a method to put the element into the array at the position index. The convention is to call this the "at:put:" method, where each parameter is preceded by a keyword of the method name. Methods which take parameters are called keyword methods (not to be confused with language keywords). The the keywords or operators which uniquely identify a method are referred to as the method selector. For example: "+", "at:put:", and "increment" are all method selectors.

Method (i.e., function) declarations are supported for actor classes using the behaviour function editor, and for data classes using the data class editor. There are three kinds of method declarations:

  1. Unary, which is a method with no arguments ( printString)
  2. Binary, which is an operator with one argument (+ <aNumber>)
  3. Keyword, which is a method which has one or more arguments (at: index put: element).

Methods can be inherited or defined locally in an actor or data class. The declaration of an RPL method begins with the method selector declaration, followed by declaration of local variables, and then the code body. A method can return an object as its result using a return statement. The default return value of a method is nil if there is no explicit return statement.

The RPL statements SELF, SUPER, and CALLSUPER can be used within a method to select a method on an instance of the current class or its superclass.

Unary method

A unary method is a method which has no arguments. For example, if a SequenceClass has a single field, then the unary printFieldString method:

printFieldString
| |
^ field printString

would return field as a string. The code fragment:

mySequence printFieldString

is then equivalent to:

mySequence field printString

SELF

An RPL code segment in an actor or data class can invoke a function or method, within an instance of a class, using the invocation statement `SELF <selector>, where the selector is the method name with arguments. For example the method:

myPrintString
| |
^ `+++ `, SELF printString

would prefix the string "+++ " to a class's default printString method. At run-time, the method that is called is the first method of that name found (e.g., printString) going up the class inheritance hierarchy, starting with the class of the actor or data object. If no method of that name is found, a run-time error occurs.

SUPER

A locally defined method can be defined with the same name as an inherited method (in which case the locally defined method overrides the inherited method). A locally defined method which overrides an inherited method of the same name can call the method on its superclass by the invocation statement `SUPER <selector>. For example:

printString
| |
^ `+++ `, SUPER printString

overrides a classes default printString method.

CALLSUPER

Alternatively, the simpler statement CALLSUPER can be used to call the superclass method passing the same arguments as received by the caller. For example the previous example could be written:

printString
| |
^ `+++ `, CALLSUPER

In each case, a run-time error will occur if no such method is defined in an ancestor of the class where the calling method is defined.

Binary method

Binary methods take in a single argument and use one or two nonalphanumeric characters as the method name (a number of restrictions apply). Following are the permitted combinations of user definable binary methods and their restrictions:

single or double combinations of the following characters:
  ~ ! @ & * - + \ / ? = > < ,

with the following restrictions:
  == is reserved   = and -> are reserved methods for actors
  - and < are not allowed as the second character of a binary
          method, e.g., --, >-, ~<, <<)

For example to add an addition operator (+) to a sequence class which has a single integer field (aField):

+ aValue
| |
aField := aField + aValue.
^ SELF yourself

can be used in the conventional manner:

a := SimpleSequence new.
(assuming a aField is initialized by this statement)
a := a + 10.
(uses + operator defined above).

NOTE: When defining/redefining operators it is a good practice to keep consistent behavior across classes for an operator. For example, redefining the + operator to perform subtraction is confusing and not a recommended practice.

Keyword method

The following declaration could be used to define a keyword method for a collection class:

remove: anElement from: aCollection
| result |
IF (result := aCollection includes: anElement ) THEN
    aCollection remove: anElement
ENDIF.
^result

which declares the keyword method remove:from: which takes in two arguments. It removes anElement from aCollection if the element is present in the collection, and it returns a Boolean value indicating whether the element was in the collection.


Limitations:

Top

None


See also:

Top

RPL Guide


 

Copyright © 1999, ObjecTime Limited.