The main goal of Logtalk objects is the encapsulation and reuse of predicates. Instead of a single database containing all your code, Logtalk objects provide separated namespaces or databases allowing the partitioning of code in more manageable parts. Logtalk does not aim to bring some sort of new dynamic state change concept to Logic Programming or Prolog.
In Logtalk, the only pre-defined objects are the built-in objects <code>user</code>, <code>debugger</code>, and <code>logtalk</code>, which are described at the end of this section.
There are only three kinds of encapsulation entities in Logtalk: objects, protocols, and categories. Logtalk uses the term <em>object</em> in a broad sense. The terms <em>prototype</em>, <em>parent</em>, <em>class</em>, <em>subclass</em>, <em>superclass</em>, <em>metaclass</em>, and <em>instance</em> always designate an object. Different names are used to emphasize the role played by an object in a particular context. I.e. we use a term other than object when we want to make the relationship with other objects explicit. For example, an object with an <em>instantiation</em> relation with other object plays the role of an <em>instance</em>, while the instantiated object plays the role of a <em>class</em>; an object with a <em>specialization</em> relation other object plays the role of a <em>subclass</em>, while the specialized object plays the role of a <em>superclass</em>; an object with an <em>extension</em> relation with other object plays the role of a <em>prototype</em>, the same for the extended object. A <em>stand-alone</em> object, i.e. an object with no relations with other objects, is always interpreted as a prototype. In Logtalk, entity relations essentially define <em>patterns</em> of code reuse.
Logtalk allows you to work from standalone objects to any kind of hierarchy, either class-based or prototype-based. You may use single or multiple inheritance, use or forgo metaclasses, implement reflective designs, use parametric objects, and take advantage of protocols and categories (think components).
We can define a new object in the same way we write Prolog code: by using a text editor. Logtalk source files may contain one or more objects, categories, or protocols. If you prefer to define each entity in its own source file, it is recommended that the file be named after the object. By default, all Logtalk source files use the extension <code>.lgt</code> but this is optional and can be set in the configuration files. Compiled source files (by the Logtalk preprocessor) have, by default, a <code>.pl</code> extension. Again, this can be set to match the needs of a particular Prolog compiler in the corresponding configuration file. For instance, we may define an object named <code>vehicle</code> and save it in a <code>vehicle.lgt</code> source file which will be compiled to a <code>vehicle.pl</code> Prolog file.
Object names can be atoms or compound terms (when defining parametric objects, see below). Objects, categories and protocols share the same name space: we can not have an object with the same name as a protocol or a category.
Object code (directives and predicates) is textually encapsulated by using two Logtalk directives: <atitle="Consult reference manual"href="../refman/directives/object1_5.html"><code>object/1-5</code></a> and <atitle="Consult reference manual"href="../refman/directives/end_object0.html"><code>end_object/0</code></a>. The most simple object will be one that is self-contained, not depending on any other Logtalk entity:
In object-oriented programming objects are usually organized in hierarchies that enable interface and code sharing by inheritance. In Logtalk, we can construct prototype-based hierarchies by writing:
We can also have class-based hierarchies by defining instantiation and specialization relations between objects. To define an object as a class instance we will write:
In short, an object can be a <em>stand-alone</em> object or be part of an object hierarchy. The hierarchy can be prototype-based (defined by extending other objects) or class-based (with instantiation and specialization relations). An object may also implement one or more protocols or import one or more categories.
A <em>stand-alone</em> object (i.e. an object with no extension, instantiation, or specialization relations with other objects) is always compiled as a prototype, that is, a self-describing object. If we want to use classes and instances, then we will need to specify at least one instantiation or specialization relation. The best way to do this is to define a set of objects that provide the basis of a reflective system <ahref="../bibliography.html#Cointe87">[Cointe 87</a>, <ahref="../bibliography.html#Moura94">Moura 94]</a>. For example:
Note that with these instantiation and specialization relations, <code>object</code>, <code>class</code>, and <code>abstract_class</code> are, at the same time, classes and instances of some class. In addition, each object inherits its own predicates and the predicates of the other two objects without any inheritance loop problems.
If you do not need a reflective system solution but still want to use classes and instances then you can simplify the above scheme by making an object an instance of itself or, if you prefer, by making a class its own metaclass. For example:
A third alternative is to use neither metaclasses or reflective designs but instead to take advantage of the built-in object <code>logtalk</code>. This empty object can be used as a dummy root superclass. For example:
</p>
<pre>:- object(class,
specializes(logtalk)).
...
:- end_object.</pre>
<p>
We can use, in the same application, both prototype and class-based hierarchies (and freely exchange messages between all objects). We can not however mix the two types of hierarchies by, e.g., specializing an object that extends another object in this current Logtalk version.
Parametric objects have a compound term for name instead of an atom. This compound term usually contains free variables that are instantiated when sending a message to the object. The object predicates can then be coded to depend on the variables instantiation values. When an object state is set at object creation and never changed, parameters provide a better solution than using the object's database via asserts. Parametric objects can also be used to attach a set of predicates to terms that share a common functor and arity.
</p>
<p>
In order to give access to an object parameters, Logtalk provides the <atitle="Consult reference manual"href="../refman/methods/parameter2.html"><code>parameter/2</code></a> built-in local method:
Note that we can't use this method with the message sending operators (<atitle="Consult reference manual"href="../refman/control/to_object2.html"><code>::/2</code></a>, <atitle="Consult reference manual"href="../refman/control/to_self1.html"><code>::/1</code></a>, or <atitle="Consult reference manual"href="../refman/control/to_object2.html"><code>^^/1</code></a>). An alternative solution is to use the built-in local method <atitle="Consult reference manual"href="../refman/methods/this1.html"><code>this/1</code></a>. For example:
Both solutions are equally efficient because the runtime cost of the methods <code>this/1</code> and <code>parameter/2</code> is negligible. The drawback of the second solution is that we must check all calls of <code>this/1</code> if we change the object name.
When storing a parametric object in its own source file, the convention is to name the file after the object, with the object arity appended. For instance, when defining an object named <code>sort(Type)</code>, we may save it in a <code>sort_1.lgt</code> text file. This way it is easy to avoid file name clashes when saving Logtalk entities that have the same functor but different arity.
Compound terms with the same functor and (usually) the same number of arguments as a parametric object identifier may act as <em>proxies</em> to a parametric object. Proxies may be stored on the database as Prolog facts and be used to represent different instantiations of a parametric object parameters.
We can find, by backtracking, all defined objects by calling the <atitle="Consult reference manual"href="../refman/builtins/current_object1.html"><code>current_object/1</code></a> built-in predicate with an uninstantiated variable:
An object can be dynamically created at runtime by using the <atitle="Consult reference manual"href="../refman/builtins/create_object4.html"><code>create_object/4</code></a> built-in predicate:
The first argument, the name of the new object (a Prolog atom or compound term), should not match any existing entity name. The remaining three arguments correspond to the relations described in the opening object directive and to the object code contents (directives and clauses).
If we need to create a lot of (dynamic) objects at runtime, then is best to define a metaclass or a prototype with a predicate that will call this built-in predicate to make new objects. This predicate may provide automatic object name generation, name checking, and accept object initialization options.
Dynamic objects can be abolished using the <atitle="Consult reference manual"href="../refman/builtins/abolish_object1.html"><code>abolish_object/1</code></a> built-in predicate:
We can define a goal to be executed as soon as an object is (compiled and) loaded to memory with the <atitle="Consult reference manual"href="../refman/directives/initialization1.html"><code>initialization/1</code></a> directive:
The initialization goal can also be a message to <i>self</i> in order to call an inherited or imported predicate. For example, assuming that we have a <code>monitor</code> category defining a <code>reset/0</code> predicate:
Note, however, that descendant objects do not inherit initialization directives. In this context, <i>self</i> denotes the object that contains the directive. Also note that by initialization we do not necessarily mean setting an object dynamic state.
Similar to Prolog predicates, an object can be either static or dynamic. An object created during the execution of a program is always dynamic. An object defined in a file can be either dynamic or static. Dynamic objects are declared by using the <atitle="Consult reference manual"href="../refman/directives/dynamic0.html"><code>dynamic/0</code></a> directive in the object source code:
The directive must precede any predicate directives or clauses. Please be aware that using dynamic code implies a performance hit when compared to static code. We should only use dynamic objects when these need to be abolished during program execution. In addition, note that we can declare and define dynamic predicates within a static object.
Besides the relations declared in the object opening directive, the predicate definitions contained in the object may imply other dependencies. These can be documented by using the <atitle="Consult reference manual"href="../refman/directives/calls1.html"><code>calls/1</code></a> and the <atitle="Consult reference manual"href="../refman/directives/uses1.html"><code>uses/1</code></a> directives.
</p>
<p>
The <code>calls/1</code> directive can be used when a predicate definition sends a message that is declared in a specific protocol:
An object can be documented with arbitrary user-defined information by using the <atitle="Consult reference manual"href="../refman/directives/info1.html"><code>info/1</code></a> directive:
Logtalk provides five sets of built-in predicates that enable us to query the system about the possible relationships that an object may have with other entities.
</p>
<p>
The built-in predicates <atitle="Consult reference manual"href="../refman/builtins/instantiates_class2_3.html"><code>instantiates_class/2</code></a> and <atitle="Consult reference manual"href="../refman/builtins/instantiates_class2_3.html"><code>instantiates_class/3</code></a> can be used to query all instantiation relations:
Specialization relations can be found by using either the <atitle="Consult reference manual"href="../refman/builtins/specializes_class2_3.html"><code>specializes_class/2</code></a> or the <atitle="Consult reference manual"href="../refman/builtins/specializes_class2_3.html"><code>specializes_class/3</code></a> built-in predicates:
For prototypes, we can query extension relations with the <atitle="Consult reference manual"href="../refman/builtins/extends_object2_3.html"><code>extends_object/2</code></a> or the <atitle="Consult reference manual"href="../refman/builtins/extends_object2_3.html"><code>extends_object/3</code></a> built-in predicates:
In order to find which objects import which categories we can use the built-in predicates <atitle="Consult reference manual"href="../refman/builtins/imports_category2_3.html"><code>imports_category/2</code></a> or <atitle="Consult reference manual"href="../refman/builtins/imports_category2_3.html"><code>imports_category/3</code></a>:
To find which objects implements which protocols we can use the <atitle="Consult reference manual"href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/2</code></a> or the <atitle="Consult reference manual"href="../refman/builtins/implements_protocol2_3.html"><code>implements_protocol/3</code></a> built-in predicates:
Note that, if we use an uninstantiated variable for the first argument, we will need to use the <atitle="Consult reference manual"href="../refman/builtins/current_object1.html"><code>current_object/1</code></a> built-in predicate to ensure that the entity returned is an object and not a category.
We can find the properties of defined objects by calling the built-in predicate <atitle="Consult reference manual"href="../refman/builtins/object_property2.html"><code>object_property/2</code></a>:
An object may have the property <code>static</code>, <code>dynamic</code>, or <code>built_in</code>. Dynamic objects can be abolished in runtime by calling the <atitle="Consult reference manual"href="../refman/builtins/abolish_object1.html"><code>abolish_object/1</code></a> built-in predicate. An object may also have the properties <code>synchronized</code> and <code>threaded</code>, which are related to multi-threading programming.
Logtalk defines a built-in, pseudo-object named <code>user</code> that contains all user predicate definitions not encapsulated in a Logtalk entity. These predicates are assumed to be implicitly declared public.
Logtalk defines a built-in object named <code>debugger</code> which implements the Logtalk built-in debugger (see the section on <ahref="running.html#debugging">debugging Logtalk programs</a> for details). This object is virtually compiled as a prototype. Programmers may define new prototypes extending <code>debugger</code> in order to implement custom debuggers.
Logtalk defines an empty built-in object named <code>logtalk</code>, which can play the role of both a class and a prototype. It may be used to define class hierarchies without forcing the use of metaclasses or reflective designs, as illustrated above. This object supports the dynamic declaration and definition of predicates (using the dynamic database methods).