method

Gamma: Version 4.0
Prev |  | Next

# `method`

`method` -- defines a method for a given class.

## Syntax

    method class.method_name ([argument [, argument]...]) statement

## Arguments

`class`

  The class for which this method is defined.

`method_name`

  The name of the method.

`arguments`

  The argument list for this method. This does not include the implied argument self, nor is self defined when the arguments are bound as the method is called.

`statement`

  The body code statment for this method. Within this statement the special variable `self` is defined as the instance on which this method is being applied.

## Returns

A function definition of the resulting method function.

## Description

This statement defines a method function for a given class. If a method already exists for this class with this name, the previous definition will be replaced. If a method of the same name exists for any parent (base) class of the given class, it will be overridden for instances of this class only. In Gamma methods are run using the syntax:

    instance.method_name(arguments);

which is the familiar `object.method` syntax used in C++.

| The `method` syntax creates a new method for a particular class. It is an error to omit the class. The argument list for `method` is identical to the argument list for `function`.

## Example

        Gamma> class RegPolygon{sides; length;}
        (defclass RegPolygon nil [][length sides])
        Gamma> method RegPolygon.perimeter (){.sides * .length;}
        (defun RegPolygon.perimeter (self) (* (@ self sides) (@ self length)))
        Gamma> class Square RegPolygon {sides = 4;}
        (defclass Square RegPolygon [][length (sides . 4)])
        Gamma> method Square.area (){sqr(self.length);}
        (defun Square.area (self) (sqr (@ self length)))
        Gamma> sqB = new(Square);
        {Square (length) (sides . 4)}
        Gamma> sqB.length = 3;
        3
        Gamma> sqB.perimeter();
        12
        Gamma> sqB.area();
        9
        Gamma>

See the PID Controller Class section of the PID Emulator chapter in the Cogent Tools Demo and Tutorials book for an example of this statement used in context.

## See Also

Class Operators, `class`, `defun`, `new`

Prev | Home | Next
`local` | Up | `progn, prog1`

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.