Go to the first, previous, next, last section, table of contents.


Compile-time usage of objects

Author(s): Angel Fernandez Pineda.

Version: 1.11#222 (2004/5/24, 13:8:7 CEST)

Version of last change: 1.7#32 (2000/11/14, 13:13:15 CET)

This package is required to enable user code to create objects and manipulate them, as well as loading any needed class.

Usage and interface (objects)

Documentation on new declarations (objects)

DECLARATION: use_class/1:

It establishes an usage relationship between the given file (which is supposed to declare a class) and current source. Usage relationships are needed in order to enable code to create instances of the given class, and to make calls to instances derived from such class.

Since an interface is some kind of class, they may be used within this declaration but only for semantic checking porpouses. Instances will not be derived from interfaces.

use_class/1 is used in the same way as use_module/1.

Usage: :- use_class(ClassSource).

DECLARATION: instance_of/2:

Statically declares an identifier to be an instance of a given class.

It may be used as new/2 predicate except for:

For every statically declared object the given constructor will be called at program startup. Those instances may be destroyed manually, but it is not recommended.

When reloading the involved class from the Ciao toplevel shell. It may destroy statically declared instances, and create them again.

Statically declared instances must be called using a specifically designed module-qualification: ClassName(Object):Goal. For example:

    :- module(example,[main/0],[objects]).
    :- use_class(library(counter)).
    :- cnt instance_of counter(10).

    main :-
         counter(cnt):decrease(1),
         counter(cnt):current_value(X),
         display(X).

But statically written code (only) is allowed to use module-style qualifications as a macro:

    main :-
         cnt:decrease(1),
         cnt:current_value(X),
         display(X).

Notice that dynamically expanded goals such as X=cnt,X:decrease(1) will not work, use X=counter(cnt),X:decrease(1) instead.

Usage: :- instance_of(Object, Constructor).

DECLARATION: new/2:

This declaration has the same effect as instance_of/2.

Usage: :- new(Object, Constructor).

Other information (objects)

Compile-time errors are restricted to some local analysis. Since there is no type declaration in the Prolog language, there is no posibility to determine whenever a given variable will hold an instance of any class.

However, little semantic analysis is performed. User may aid to perform such an analysis by the usage of run time checks (which are also detected at compile time), or static declarations. For example:

clause(Obj) :- Obj:a_method(334).

O'Ciao may be not able to determine whenever a_method/1 is a valid method for instance Obj, unless some help is provided:

clause(Obj) :- Obj instance_of myclass,Obj:a_method(334).

In such case, O'Ciao will report any semantic error at compile-time. Most of the run-time errors are related to normal Ciao Prolog module system. Since objects are treated as normal Prolog modules at run time, there is no further documentation here about that stuff.

Error reporting at compile time (objects)

Error reporting at run time (objects)


Go to the first, previous, next, last section, table of contents.