ColdC: Defining Methods


When defining a method several considerations must be taken into account. What arguments will the method accept? What local variables will the method use? What sort of flags will the method have? What sort of access will other objects have to the method?

Arguments and Variables

The arguments and variables for a method are defined within the method code. The internal structure of a method is:

arg arg1, arg2, ..., [rest];
var var1, var2, ...;

statement1
statement2
.
.
.

The arg declaration gives a list of argument variables, whose values will correspond to the arguments passed with the message. You may omit the arg declaration if the method does not take any arguments. If the final argument variable is given in square brackets (that is, if you specify rest), then the method can accept a variable number of argments; rest will contain a list of the arguments beyond the ones corresponding to the argument variables. If you do not specify rest, then the method can accept only the number of arguments specified by the argument variables, no more.

The var declaration tells the compiler that the listed identifiers should refer to local variables. You may omit the var declaration if you do not wish to declare any local variables.

The statements statement1, statement2, ... are the body of the method. They tell the interpreter what the method does.

Method Flags

Method flags define certain behavioral features of the method. Currently the following method flags exist:

'nooverride
Methods which specify 'nooverride cannot be overridden by any of the defining object's descendants.
'lock
'lock locks all aspects of a method. Locked methods cannot have their access or flags changed, nor can they be recompiled during run-time. Locked methods can be changed outside of the regular running environment (such as in a textdb, or with coldcc).
'fork
'fork is used to specify that the method forks from the current task. The return value of a forked method is the task id of the new task (fork is currently unsupported).
'native
'native specifies a native method. Native methods are not really methods, but are actually functions acting as a method. Because of this native methods cannot be listed or manipulated in most of the usual ways.

All method flags, with the exception of 'native, can be manipulated using the functions method_flags() and set_method_flags().

Method Access

It is possible to restrict what calls a method by setting the method's access. By default all methods are public methods. The available settings for method access are:

'public
This access state is the default. A public method can be called by any object.
'protected
Protected methods can only be called by the defining object, or descendants of the defining object (sender() must be this()).
'private
Private methods can only be called by the object they were defined on (caller() must be this()).
'root
Root methods can only be called by the $root object (caller() must be $root).
'driver
Driver methods can only be called by the driver.

Method access can be manipulated using the functions method_access() and set_method_access().


Last Modified on Feb 26 1996
Copyright © 1995, 1996, Brandon Gillespie