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


Run time usage of objects

Author(s): Angel Fernandez Pineda, Angel Fernandez Pineda.

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

Version of last change: 1.7#51 (2001/1/25, 21:33:0 CET)

This library provides run-time support for object creation and manipulation. Objects are also called class instances, or simply instances.

Objects in Ciao are treated as normal modules. This is, an object is a run-time generated Prolog module, which may be identified by an unique term across the whole application.

This is a very simple example of how to create an instance, and how to make calls to it:

            AnObj new myclass,
            AnObj:mymethod.

In order to make any object accessible from code, an usage relationship must be established between the class (from which instances are derived) and the code itself. Refer to use_class/1 predicate or use_class/1 declaration in order to do so.

Usage and interface (objects_rt)

Documentation on exports (objects_rt)

PREDICATE: new/2:

Dynamic instance creation takes place by the ways of this predicate.

It takes a free variable as first argument which will be instantiated to an internal object identifier.

Second argument must be instantiated to a class constructor. Class constructors are designed to perform an initialization on the new created instance. Notice that instance initialization may involve some kind of computation, not only state initialization.

A class constructor is made by a functor, which must match the intended class name, and any number of parameters. For example:

            Obj new myclass(1500,'hello, world!!!')

Those parameters depends (obviously) on the constructors defined at the class source. If no constructors where defined, no parameters are needed. This is called the default constructor. An example:

            Obj new myclass

The default constructor can not be called if there is any constructor available at the class source.

Instantiation will raise an exception and fail whenever any of this conditions occur:

Objects may also be statically declared, refer to instance_of/2 declaration.

Usage: new(InstanceVar, Constructor)

PREDICATE: instance_of/2:

This predicate is used to perform dynamic type checking. You may check whether a particular instance belongs to a particular class or related descendants.

instance_of/2 is used to perform static semantic analisys over object oriented code constructions.

By the use of instance_of/2 you may help to perform such analisys.

Usage 1: instance_of(Instance, Class)

Usage 2: instance_of(Instance, Class)

PREDICATE: derived_from/2:

Test whether an object identifier was derived directly from a class, by the usage of new/2 or a static instance declaration ( instance_of/2).

Usage 1: derived_from(Instance, Class)

Usage 2: derived_from(Instance, Class)

PREDICATE: interface/2:

This predicate is used to ensure a given interface to be implemented by a given instance.

Usage 1: interface(Instance, Interface)

Usage 2: interface(Instance, Interfaces)

PREDICATE: instance_codes/2:

Retrieves a character string representation from an object identifier and vice-versa.

Usage 1: instance_codes(Instance, String)

Usage 2: instance_codes(Instance, String)

PREDICATE: destroy/1:

As well as instances are created, they must be destroyed when no longer needed in order to release system resources.

Unfortunately, current O'Ciao implementation does not support automatic instance destruction, so user must manually call destroy/1 in order to do so.

The programmer must ensure that no other references to the involved object are left in memory when destroy/1 is called. If not, unexpected results may be obtained.

Usage: destroy(Instance)

PREDICATE: use_class/1:

The behaviour of this predicate is identical to that provided by the declaration of the same name use_class/1. It allows user programs to dynamically load classes. Whether the given source is not a class it will perform a use_module/1 predicate call.

Usage: use_class(ClassSource)

PROPERTY: constructor/1:

Usage: constructor(Cons)

PROPERTY: class_name/1:

Usage: class_name(ClassName)

PROPERTY: interface_name/1:

Usage: interface_name(Interface)

PROPERTY: instance_id/1:

Usage: instance_id(ID)

PROPERTY: class_source/1:

Usage: class_source(Source)

PROPERTY: interface_source/1:

Usage: interface_source(Source)

PROPERTY: method_spec/1:

There is no difference between method or attribute specifications, and habitual predicate specifications. It is just a Functor/Arity term.

Usage: method_spec(Spec)

PROPERTY: virtual_method_spec/1:

Usage: virtual_method_spec(Spec)

Known bugs and planned improvements (objects_rt)


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