Search code examples
listobjectnumbersprotocolslogtalk

Number of objects in Logtalk


I've got the protocol:

:- protocol(person).
:- public([name/1,
        age/1]).
:- end_protocol.

For example, I've made unknown number of objects by using create_object/4, how can I get a number of them? It is not a problem to get their names by current_object/1, but I need an integer!


Solution

  • Assuming only objects (i.e. no categories) implement the person protocol, you can compute their number using e.g.

    count(N) :-
        findall(1, implements_protocol(_,person), L),
        list::length(L, N).
    

    Replace the call to implements_protocol /2 with conforms_to_protocol/2 if you have hierarchies of objects. You can also generalize the count/1 predicate by passing the protocol as an argument.