use objects, but define them later

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
joerg
Posts: 40
Joined: Fri Dec 21, 2007 9:38 am

use objects, but define them later

Post by joerg » Thu Jun 26, 2008 10:18 am

Hello,

In a program of mine, I want the user to be able to modify certain logtalk
objects o1, o2, o3, ... Therefore, these objects are in a file that the user is
supposed to edit, let us call it 'data.lgt'. On the other hand, the program,
'compiler.lgt', manipulates o1, o2 and o3. Therefore, none of the following
initializations is adequate:

Code: Select all

:- initialization((logtalk_load([compiler, data]))). 
:- initialization((logtalk_load([data, compiler]))). 
Although both initializations work, I will get warnings about references to
unknown objects.

What would be the most logtalkish way to avoid such warnings?

Jörg

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

Re: use objects, but define them later

Post by Paulo Moura » Thu Jun 26, 2008 10:33 am

joerg wrote:...
Although both initializations work, I will get warnings about references to
unknown objects.

What would be the most logtalkish way to avoid such warnings?
Use the logtalk_load/2 predicate instead with the option "unknown" set to silent.

Cheers,

Paulo
Paulo Moura
Logtalk developer

joerg
Posts: 40
Joined: Fri Dec 21, 2007 9:38 am

Re: use objects, but define them later

Post by joerg » Thu Jun 26, 2008 10:53 am

Hello Paulo,

This would certainly be a solution. Yet, it has the disadvantage that Logtalk will not warn me in those cases where I use undefined objects unintentionally.

I was able to avoid avoid such warnings sometimes by using categories instead of objects:

Code: Select all

% File: compiler.lgt

:- category(word_compiler).
    
    compile(Compiled) :-
        ::name(Name),
        do_something_with_name(Name, Compiled).

:- end_category.

% File: data.lgt

:- object(house),
    extends(word),
    imports(word_compiler)).
    
    name(house).

:- end_object.

What I was looking for is a way to do something similar with objects ... I.e. I would like to define some object o1 in file f1 and then extend the definition of o1 in file f2 in a clean way i.e. without redefining it.

But your solution is certainly an option. Thank you for your answer.


Jörg

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

Re: use objects, but define them later

Post by Paulo Moura » Thu Jun 26, 2008 11:14 am

joerg wrote: ...
What I was looking for is a way to do something similar with objects ... I.e. I would like to define some object o1 in file f1 and then extend the definition of o1 in file f2 in a clean way i.e. without redefining it.
You can use a category to extend an object that does not import the category. I.e. a category may complement an existing object without modifying its source code. This is handy when e.g. you want to add some predicates to an existing third-party library object. Check the user manual section on categories and the "complements" example.

Cheers,

Paulo
Paulo Moura
Logtalk developer

joerg
Posts: 40
Joined: Fri Dec 21, 2007 9:38 am

Re: use objects, but define them later

Post by joerg » Thu Jun 26, 2008 11:50 am

user manual section on categories and the "complements" example.
This is exactly what I was looking for. I did not remember this section, because, when I read it, I had no experience in Logtalk at all and did not know what it could be good for.

Thanks for the tip and for the Logtalk feature. Every time I think that this or that feature might be handy, it turns out that Logtalk already has it.

Jörg

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest