I would like to abort loading a file/module if a necessary precondition isn’t met, e.g., SWI-Prolog version doesn’t support a required feature. Is there a simple way to do this using directives? (or some other way?)
I’ve tried an initialization goal that fails or generates an exception, but the load just continues anyway. I’ve also tried conditional compilation, but end up with undefined error messages for all the exported predicates.
What I’d like to do is output an error message and abort the load.
Create a foreign “blob” whose load function always fails. Let’s call it loadfail blob. It’s about a dozen lines of code(*), plus a few lines for a foreign predicate that we’ll call make_loadfail/1 that creates an instance of the loadfail blob.
You can then create a blob and save it in a .qlf file using this code (loadfail.pl):
When you want to abort the load process, do load_files(['loadfail.qlf']). This should abort the load process – it might also generate a fatal error.
(*) I’d write the dozen or so lines myself and try this out, but my house is without power and I’m taking turns at the local library where all the power outlets are in use and the WiFi is rather overloaded. Power is supposed to back on by Saturday, so I can try this out then if you wish.
There is no way to abort a load. If you only want code loaded under some condition, normally yo9u do so before deciding to load, but it also works to use this, i.e., the module may be in conditional compilation.
ERROR: Unknown procedure: cm_test/1 (DWIM could not correct goal)
I’m guessing the parser still has to parse the text after :- else. looking for the :- endif.. In doing so, everything gets tossed, including the style-check directive. But any parser errors are still output, such as those due to operator definitions that were discarded.
Is there a way to suppress these messages on code that isn’t loaded? Note: fixing it now isn’t helpful because I would like this to work on old releases of SWIP.
Conditional compilation is defined on Prolog terms and not on a different language as for example C(++) uses. There may be something to say for not emitting syntax errors and singletons while in a “false” branch. But then, I assume there is some condition under which the code is loaded and you’ll get the same warning.
The situation is more severe if you want to load code using some syntax extension conditionallty, e.g. not loading code holding 1r3 if rationals are not supported. Suppressing syntax errors in false branches seems fhe opnly way out.