users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Conditional includes...


From: Paulo Moura
Subject: Re: Conditional includes...
Date: Sat, 9 Nov 2013 22:38:00 +0000

On 09/11/2013, at 22:30, Sean Charles <address@hidden> wrote:

> I am having troubles with files being included more than once and I don’t 
> seem to be able to code my away around it!
> 
> In my utils.pl file I have a predicate called now_string() and I have 
> application code and test scripts that use it and now, after refactoring, my 
> test scripts break because none of the files under test are pulling it in… I 
> removed it from one file because that file was being included in another file 
> yadda yadda yadaa you know how these things get from time to time.
> 
> I thought I could put in a test like this:
> 
> :-if( \+ current_predicate(utils_included)).
> :-include('utils.pl').
> :-endif.
> 
> But it won’t play ball. In the utils.pl file the first fact is 
> “utils_included.” I hope you get the picture, problem is, gprolog doesn’t!
> 
> compiling /Users/seancharles/Documents/FELT/felt-prolog/feltlexer.pl for byte 
> code...
> /Users/seancharles/Documents/FELT/felt-prolog/feltlexer.pl:38:1: syntax 
> error: . or operator expected after expression
> /Users/seancharles/Documents/FELT/felt-prolog/feltlexer.pl:39: fatal error: 
> unexpected endif directive
> 
> Is there an “accepted technique” for handling file included in Prolog 
> applications to avoid multiple definitions and things?

Try:

:- if(\+ current_predicate(utils_included/0)).
:- initialization(consult('utils.pl')).
:- endif.

The initialization/1 is deferred until the file is loaded so you shouldn't get 
an error related to a missing endif/0 directive that, I assumed is that to some 
implementation issue with the include/1 directive. Also note that 
current_predicate/1 takes (or returns) a predicate indicator as argument, not a 
predicate functor.

Cheers,

Paulo

-----------------------------------------------------------------
Paulo Moura
Logtalk developer

Email: <mailto:address@hidden>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------







reply via email to

[Prev in Thread] Current Thread [Next in Thread]