Prototype-based object extension and composition

Suggestions for new Logtalk features

Moderator: Paulo Moura

Post Reply
plmday
Posts: 3
Joined: Tue Jul 02, 2013 12:14 pm

Prototype-based object extension and composition

Post by plmday » Mon Jul 08, 2013 9:17 am

It seems to me the current prototype-based OO-system does not support object extension and composition. By object-extension, I mean real extension as follows:

Code: Select all

:- object(a).
    :- public(app/3).

    app([], Ys, Ys).
:- end_object.

:- object(b, extends(a)).
    :- public(app/3).

    app([X | Xs], Ys, [X | Zs]):- app(Xs, Ys, Zs).
:- end_object.
After loading the source, I would like to expect that a query like

Code: Select all

?- b::app([1, 2], [3, 4], Zs).
Zs = [1, 2, 3, 4].
In general, I would like a prototype-based OO-system support object composition, similar to mixin composition.
Last edited by plmday on Mon Jul 08, 2013 12:12 pm, edited 1 time in total.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Prototype-based object extension and composition

Post by Paulo Moura » Mon Jul 08, 2013 10:58 am

In Logtalk, as illustrated in your example, the extends relation is a relation between a prototype and its parent(s). For mixing-like programming, you can use categories (see the documentation and the examples). In the specific case of your example, however, you can use a "super" call in the derived prototype to access clauses for the app/3 predicate in its parent:

Code: Select all

app(List1, List2, List) :- ^^app(List1, List2, List).
Otherwise, the definition of the predicate app/3 in the derived prototype simply overrides the inherited definition. See the "Inheritance" section on the User Manual.
Paulo Moura
Logtalk developer

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest