-1

i have :

:- use_module(library(logtalk)).
:- {buffer}.
:- initialization(main).

main :-
    create_object(env,[instantiates(buffer)],[],[]), 

it works but every time I re-consult the file it barks an error because the object already exists.

Is there a way to check if the object already exists and skip recreating it ?


more code:

:- use_module(library(logtalk)).

:- consult(utils).

%% :- initialization((
%%     logtalk_load([buffer,env]).
%% )).
:- {buffer}.
:- initialization(main).

main :-
    %% create_object(env,[instantiates(buffer)],[],[]), 
    env::set(uid,0), env::set(name,"").

this worked :

:- initialization((
    logtalk_load([buffer])
)).

i.e. no dot and no "env"

sten
  • 7,028
  • 9
  • 41
  • 63

1 Answers1

1

You can use the current_object/1 predicate to check if an object exists. But from your code fragment is seems that you could simply define the env object in a source file. If you need env to be a dynamic object (why?), then use the dynamic/0 directive:

:- object(env,
    instantiates(buffer)).

    :- (dynamic)/0.

:- end_object

Btw, never use top-level abbreviations (such as {}/1) in source files; they are not part of the language. Write instead:

:- use_module(library(logtalk)).
:- initialization((
    logtalk_load([buffer, env])
)).

P.S. You're using the logtalk pack for SWI-Prolog. But this pack meant for deployment, not development, as it hides all files on the Logtalk distribution (including documentation) inside the hidden directory used for packs.

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
  • i have other stuff in "main" – sten Jan 18 '21 at 19:57
  • Note that you can have multiple `initialization/1` directives in the same source file. – Paulo Moura Jan 18 '21 at 19:59
  • i had env in buffer.lgt but i thought it will be better in the source file. may be u are right i dont need it in the source cause it is not dynamic – sten Jan 18 '21 at 20:00
  • whats the benefit of not using pack ? – sten Jan 18 '21 at 20:01
  • {buffer} works the other doesnt.... i'm using env in main – sten Jan 18 '21 at 20:06
  • How are you using the `env` object in `main/0`? What errors do you get? Can you expand the question with those details? – Paulo Moura Jan 18 '21 at 20:12
  • There's nothing in your edited code that would trigger an error when using an `initialization /1` directive to load the files. The initialization goals are run in order. Thus, by the time you call `main/0`, the `buffer` and `env` source files are already loaded. If you're getting any error or warning with my suggestion, add it to your question to clarify. – Paulo Moura Jan 18 '21 at 23:19
  • i put env in buffer not as separate file ;) – sten Jan 18 '21 at 23:42
  • The dot as a typo indeed. Fixed. But no reason why having the objects in separate files would cause an error or warning. As far as I understand, `env` depends on `buffer`. so, as long as you load `buffer` before the `env`, you should get a clean compilation. – Paulo Moura Jan 19 '21 at 00:18