::METHOD

The ::METHOD directive creates a method object and defines the method attributes.

>>-::METHOD--methodname--+-------+--+-----------+--+----------+--->
                         +-CLASS-+  +-ATTRIBUTE-+  +-ABSTRACT-+

    +-PUBLIC--+  +-GUARDED---+  +-UNPROTECTED-+
 >--+---------+--+-----------+--+-------------+--;----------------><
    +-PRIVATE-+  +-UNGUARDED-+  +-PROTECTED---+

Note: You can specify all options in any order.

A ::METHOD directive starts a method, which is ended by another directive or the end of the program. The ::METHOD is not included in the method source.

The methodname is a literal string or a symbol that is taken as a constant. The method is defined as methodname in the class specified in the most recent ::CLASS directive. Only one ::METHOD directive can appear for any methodname in a class.

A ::CLASS directive is not required before a ::METHOD directive. If no ::CLASS directive precedes ::METHOD, the method is not associated with a class but is accessible to the main (executable) part of a program through the .METHODS built-in object. Only one ::METHOD directive can appear for any method name not associated with a class. See .METHODS for more details.

If you specify the CLASS option, the method is a class method. See Objects and Classes. The method is associated with the class specified on the most recent ::CLASS directive. The ::CLASS directive is required in this case.

If you specify the PRIVATE option, the method is a private method. (Only a message the same object sends can activate the method.) If you omit the PRIVATE option or specify PUBLIC, the method is a public method that any sender can activate.

If you specify the UNGUARDED option, the method can be called while other methods are active on the same object. If you do not specify UNGUARDED, the method requires exclusive use of the object variable pool; it can run only if no other method that requires exclusive use of the object variable pool is active on the same object.

If you specify the ATTRIBUTE option, in addition to having a method created as methodname in the class specified in the most recent ::CLASS directive, another method is also automatically created in that same class as methodname=.

For example, the directive

::method name attribute

creates two methods, NAME and NAME=. The NAME and NAME= methods are equivalent to the following code sequences:

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

::method name
  expose name
  return name

If you specify the ABSTRACT option, the method creates an ABSTRACT method placeholder. ABSTRACT methods define a method that an implementing subclass is expected to provide a concrete implementation for. Any attempt to invoke an ABSTRACT method directly will raise a SYNTAX condition.

If you specify the PROTECTED option, the method is a protected method. (See The Security Manager for more information.) If you omit the PROTECTED option or specify UNPROTECTED, the method is not protected.

If you specify ATTRIBUTE or ABSTRACT, another directive (or the end of the program) must follow the ::METHOD directive.

Example:

r = .rectangle~new(20,10)
say "Area is" r~area       /* Produces "Area is 200" */

::class rectangle

::method area              /* defined for the RECTANGLE class */
  expose width height
  return width*height

::method init
  expose width height
  use arg width, height

::method perimeter
  expose width height
  return (width+height)*2

Note: It is an error to specify ::METHOD more than once within the same class and use the same methodname.