[top] [up] [next]

Declaring classes


To define a prolog class the operators class and endclass must be used. Each of these operators must be followed by the name of the class. Below there is an example of a class definition of a class named stack.

    class stack.
    endclass stack.

Like with any prolog clause each statement must be terminated by a dot ('.') and the name of the class must be an atom; the name must start with a lowercase character and the remaining characters must be digits, lowercase or uppercase letters or underscore characters. Between the clauses with the class and endclass operators  the contents of the class must be declared. The methods of the class must be defined outside the class.

A class can be defined public or private, a public class can be used by other modules than the module in which it was defined, a private class can only be used by other classes and/or predicates in the same module. To make a class public use the public/0 predicate in front of the class declaration. Like normal prolog clauses a class is defined as private if public/0 is not used.

    public.

    class stack.
    endclass stack.

More than one class can be defined inside a single module file but it is not possible to define classes inside other classes.

Attributes

Attributes are defined by using the operator parts. After the parts operator a single attribute can be declared or a comma separated expression of attributes may follow. Each attribute must be a prolog variable; the name of the attribute must start with a capital letter or an underscore character then followed by digits, lowercase or uppercase letters or underscore characters. Below there is an example of a class with two attributes.

    public.

    class stack.
      parts List, Count. %All attributes are prolog variables
    endclass stack.

It is also possible to define attributes that are instances of other classes. To define such an attribute specify the name of the class followed by the scope operator :: and then the name of the attribute, in the example below a class attribute is declared.

    public.

    class queue.
       parts elements::List. %Declare attribute that is an instance of class 'elements'.
    endclass queue.

Methods

Like attributes must be declared so must all methods of a class be declared. A method declaration consists of the name of the method (must be an atom) followed by a predicate indicator operator and the arity of the method (the number of arguments). Below there is an example of a class with several method declarations.

    public.

    class stack.
      public.
      init/0. %Method called 'init' with 0 arguments
      init/2. %Method called 'init' with 2 arguments
      push/1.
      pop/1.
      contains/1.
    private.
      headstack/3. %A private method of the class
      parts List, Contains.
    endclass stack.

As can be seen from the example above it is possible for different methods to use the same name as long as their arity is different. Methods can also be declared public or private. A public method is accessible from outside the class in which it was defined, private methods can only be called by other methods of the same class. So the method headstack/3, from the example above, can only be called by the methods init/0, init/2, push/1, pop/1 and contains/1 of the stack class.

All methods of a class must be declared inside the class declaration but the definition of each method must be placed outside the class declaration.

Remark: other OO languages like C++ and Delphi's ObjectPascal offer the ability to define virtual methods so that the correct method implementation is called even if a pointer to a base class is used. With Trinc-Prolog it is not necessary to define virtual methods because all methods declared are treated as virtual methods and the inference engine of Trinc-Prolog always knows the correct type of an instance of a class, even when methods of superclasses are being executed.

Declaring an abstract class

An abstract class can be declared by using the keyword abstractclass, an abstract class is a class of which no instances can be created, below there is an example of declaring a class abstract.

    public.

    abstractclass stackprotocol.
      public.
      make_empty/0. %Makes the stack empty
    endclass stackprotocol.

Abstract classes are usefull for implementing general and re-usable methods.

Declaring a singleton class

An singleton class can be declared by using the keyword singletonclass, a singleton class is a class of which exactly 1 instance can exist.

    public.

    singletonclass clipboard.
      public.
      paste/1.
      copy/1.
      make_empty/0. %
    endclass clipboard.

Singleton classes are usefull for implementing classes that control some kind of (external) resource of which only instance can exist. For instance: there is always a single clipboard in an application so the class 'clipboard' is declared a singletonclass.

If a singletonclass is used as a superclass for another class then it is still not allowed to create more than 1 one instance of the derived class.

Arrow.gif (1632 bytes) If a class is a singleton class then all (direct or indirect) derived classes are also singletonclasses.

[top] [up] [next]

 

info@trinc-prolog.com