Object instances in Logtalk

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
Monikuta
Posts: 13
Joined: Mon May 12, 2008 9:02 am

Object instances in Logtalk

Post by Monikuta » Mon May 19, 2008 5:55 pm

Hello,
I want to create in my object "main" instances of type "student". I have read the Logtalk documentation and looked through the examples that come with Logtalk, but I still do not understand :(

Code: Select all

:- object(main).
      start :-
          // here I want to make S new object student
:- end_object.

:- object(student,
          instantiates(student)).

   % get, set or test id
   :- public(id/1).
   :- mode(id(?integer), zero_or_one).

   % get, set or test name
   :- public(name/1).
   :- mode(name(?symbol), zero_or_one).
   
   % get, set or test mean
   :- public(mean/1).
   :- mode(mean(?float), zero_or_one).
:- end_object.
Could you please give me an advice? I look forward to your response.
Yours sincerely,
Monica Dogaru

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

Re: Object instances in Logtalk

Post by Paulo Moura » Mon May 19, 2008 8:03 pm

Monikuta wrote:Hello,
I want to create in my object "main" instances of type "student". I have read the Logtalk documentation and looked through the examples that come with Logtalk, but I still do not understand :(
Logtalk provides a built-in predicate, create_object/4, that allows creation of new objects at runtime. This predicate is usually used as a primitive for more sophisticated behavior (e.g. for implementing creation of class instances; check the "roots" example). Note that Logtalk supports both prototype-based and class-based object designs. The built-in predicate create_object/4 may be used for both. Objects, including class instances may also be defined in a source file that you compile and load. If you provide a description of what you're trying to accomplish I should be able to provide you with more specific guidance. Nevertheless, try the following small example:

Code: Select all

:- object(main).

    :- initialization(start).

    start :-
        student::new(Student),
        write('New student instance: '), writeq(Student), nl.

:- end_object.

:- object(student,
    instantiates(class),
    specializes(object)).

:- end_object.
Save the above code in e.g. a "student.lgt" source file, start Logtalk and try the following query:

Code: Select all

{roots(loader), student}.
Keep in mind that there is nothing special on the predicate new/1 being used above. Or, for that matter, on the code on the "roots" example. You can design your own instance creation methods to fit your application requirements.

Hope this helps. Best regards,

Paulo
Paulo Moura
Logtalk developer

Monikuta
Posts: 13
Joined: Mon May 12, 2008 9:02 am

Re: Object instances in Logtalk

Post by Monikuta » Mon May 19, 2008 8:47 pm

This was very useful, thank you very much :D
To begin with, I am trying some small examples in SWI-Prolog with Logtalk, just to accomodate with the syntax.
The next step is continuing with my bachelor's degree project in Logtalk and JIProlog :)
I will come back with details and questions.
I am sincerely greatful for your help.
Yours faithfully,
Monica Dogaru

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest