Messages

Messages allows us to call object predicates. Logtalk uses the same nomenclature found in other object-oriented programming languages such as Smalltalk. Therefore, the terms predicate and method are often used interchangeably when referring to predicates defined inside objects and categories. A message must always match a predicate within the scope of the sender object.

Note that message sending is only the same as calling an object’s predicate if the object does not inherit (or import) predicate definitions from other objects (or categories). Otherwise, the predicate definition that will be executed may depend on the relationships of the object with other Logtalk entities.

Operators used in message sending

Logtalk declares the following operators for the message sending control constructs:

:- op(600, xfy, ::).
:- op(600,  fy, ::).
:- op(600,  fy, ^^).

It is assumed that these operators remain active (once the Logtalk compiler and runtime files are loaded) until the end of the Prolog session (this is the usual behavior of most Prolog compilers). Note that these operator definitions are compatible with the predefined operators in the Prolog ISO standard.

Sending a message to an object

Sending a message to an object is accomplished by using the ::/2 control construct:

..., Object::Message, ...

The message must match a public predicate declared for the receiving object. The message may also correspond to a protected or private predicate if the sender matches the predicate scope container. If the predicate is declared but not defined, the message simply fails (as per the closed-world assumption).

Delegating a message to an object

It is also possible to send a message to an object while preserving the original sender by using the []/1 delegation control construct:

..., [Object::Message], ....

This control construct can only be used within objects and categories (at the interpreter top-level, the sender is always the pseudo-object user so using this control construct would be equivalent to use the ::/2 message sending control construct).

Sending a message to self

While defining a predicate, we sometimes need to send a message to self, i.e., to the same object that has received the original message. This is done in Logtalk through the ::/1 control construct:

..., ::Message, ....

The message must match either a public or protected predicate declared for the receiving object or a private predicate within the scope of the sender otherwise an error will be thrown (see the Reference Manual for details). If the message is sent from inside a category or if we are using private inheritance, then the message may also match a private predicate. Again, if the predicate is declared but not defined, the message simply fails (as per the closed-world assumption).

Broadcasting

In the Logtalk context, broadcasting is interpreted as the sending of several messages to the same object. This can be achieved by using the message sending method described above. However, for convenience, Logtalk implements an extended syntax for message sending that may improve program readability in some cases. This extended syntax uses the (,)/2, (;)/2, and (->)/2 control constructs. For example, if we wish to send several messages to the same object, we can write:

| ?- Object::(Message1, Message2, ...).

This is semantically equivalent to:

| ?- Object::Message1, Object::Message2, ... .

This extended syntax may also be used with the ::/1 message sending control construct.

Calling imported and inherited predicates

When redefining a predicate, sometimes we need to call the inherited definition in the new code. This functionality, introduced by the Smalltalk language through the super primitive, is available in Logtalk using the ^^/1 control construct:

..., ^^Predicate, ....

Most of the time we will use this control construct by instantiating the pattern:

Predicate :-
    ...,            % do something
    ^^Predicate,    % call inherited definition
    ... .           % do something more

This control construct is generalized in Logtalk where it may be used to call any imported or inherited predicate definition. This control construct may be used within objects and categories. When combined with static binding, this control construct allows imported and inherited predicates to be called with the same performance of local predicates. As with the message sending control constructs, the ^^/1 call simply fails when the predicate is declared but not defined (as per the closed-world assumption).

Message sending and event generation

Every message sent using the ::/2 control construct generates two events, one before and one after the message execution. Messages that are sent using the ::/1 (message to self) control construct or the ^^/1 super mechanism described above do not generate any events. The rationale behind this distinction is that messages to self and super calls are only used internally in the definition of methods or to execute additional messages with the same target object (represented by self). In other words, events are only generated when using an object’s public interface; they cannot be used to break object encapsulation.

If we need to generate events for a public message sent to self, then we just need to write something like:

Predicate :-
    ...,
    % get self reference
    self(Self),
    % send a message to self using ::/2
    Self::Message,
    ... .

If we also need the sender of the message to be other than the object containing the predicate definition, we can write:

Predicate :-
    ...,
    % send a message to self using ::/2
    % sender will be the pseudo-object user
    self(Self),
    {Self::Message},
    ... .

When events are not used, is possible to turn off event generation globally or on a per entity basis by using the events compiler flag (see the Event-driven programming section for more details).