Re: Conditional includes...

Paulo Moura <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
On 09/11/2013, at 22:30, Sean Charles <[email protected]> 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:[email protected]>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.