-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ADDED: library(options) #1134
base: master
Are you sure you want to change the base?
ADDED: library(options) #1134
Conversation
This library is useful mostly in other libraries, to more easily reason about options of various predicates. Currently, the only provided predicate is option/3 to ask for the value of an option, and use a default value if the option is not specified explicitly.
What happens with an unexpected option? Please note that options have been added in Cor.3. It seems you only support certain instantiation errors. |
Yes, this is very preliminary currently, limited to selecting the needed option! Type checks for options can be added with suitable declarations, maybe with a second predicate that goes through the options. The check may even be performed statically in some cases, using goal expansion. |
An example of an |
So far, options are producing arbitrary type and domain errors, but with an incorrect option, there is only one domain_error(xxx_option,...) the additional in some sense more precise error needs to be caught and then put in the 2nd argument of error/2. |
OK, I have converted this pull request to a draft for the time being, maybe there is a better or more comprehensive way to solve this. |
2a7a1b7
to
bb624cc
Compare
cf96173
to
bb420e9
Compare
0666b56
to
dff2e73
Compare
While working on Prolog code, I've also wrote my A user of a library should be able to retrieve a list of default values: main :-
library_predicate("Hello", Options),
portray_clause(Options). % Should print a list of default values. A library writer should be able to define if she wants to allow or forbid unknown options: library_predicate(Greeting, Options) :-
Defaults = [padding(true),encoding(utf8)|_], % Partial list allows any additional unknown option
options(Options, [Padding,Encoding|_], Defaults),
... It might be useful for writing forward compatible libraries. An option may be any acyclic term, because sometimes you need larger arities to better describe it: options(_, _, [padding(left,30)]). |
This library is mostly intended for authors of other libraries. I did not even mention it in the
README
, because it only contains a single predicate:option/3
, to ask whether an option was specified, obtaining its value, and to use a default if the option is not stated explicitly.Still, I think this is a useful building block, and makes option processing in various libraries easier, more correct, and less error-prone. I have used it in
library(crypto)
andlibrary(charsio)
to show how it works.