[top] [up] [next]

Defining class methods


The definition of a method, this is the prolog source of a method, is always placed outside the class declaration, the difference between a normal prolog clause and a prolog method is that the name of the class and the scope operator are placed in front of the functor of the clause. Below there is an example of the init/0 method of the stack example.

  stack::init :-
    List := [],
    Contains := 0. %Assignment operator used to initialise the two instance variables

As you can see from the example above the names of the variables 'List' and 'Contains' are equal to the names of the attributes in the class definition of the stack class, this means that the values assigned to these variables are in fact assigned to the attributes of an instance.

Instance variables

Each variable declared as an attribute of a class by the parts operator inside a class declaration is considered a class variable and if the same name is used for another variable in a method definition then the two variables are considered the same variable. If for instance a variable 'Top' is declared then all the variables with the name 'Top' of the class methods are in fact the variable declared in the class.

public.

class car.
    parts Weight, NumPassengers.
  public.
    init/2.
    calcAverageWeight/1.
  private.
endclass car.

car::init( W, Num ) :-
  Weight is W,
  NumPassengers is Num.

car::calcAverageWeight( Output ) :-
    Output is Weight + (NumPassengers * 70).

The init/2 method in the example above contains four variables, the class declaration contains no attributes with the names 'W' and 'Num' so these variables are local to the method init/2. The names of the other two variables are identical to attributes declared by the class so both these variables are treated as instance attributes; values assigned to these attributes are in fact assigned to the attributes of the instance which is executing the method init/2.
It is not required that a method uses any instance attributes at all, a method of a class can use only local variables.

Matching with class methods

Just like with normal prolog clauses it is possible to have several definitions for the same method, below there is an example of such a clause. The example displays the prolog source of the private method headstack/3 of the stack example. The purpose of this method is to get the first element of a list.

    stack::headstack([Head|Tail], Head, Tail). %List contains more than 1 element

    stack::headstack([Head], Head, []). %List contains a single element

The order of definition is as with normal prolog clauses relevant, the prolog inference engine sees the upper headstack definition first. A match is attempted with the first definition found, if the match fails then a match is attempted with the second definition. If a match with the first definition succeeds then during a redo of a subgoal the second definition can be used for a match.

In the headstack/3 example no names are similar to the names of attributes in the class declaration so these variables are all local variables.

[top] [up] [next]

 

info@trinc-prolog.com