The library(option)
provides some utilities for
processing option lists. Option lists are commonly used as an
alternative for many arguments. Examples of built-in predicates are open/4
and write_term/3.
Naming the arguments results in more readable code, and the list nature
makes it easy to extend the list of options accepted by a predicate.
Option lists come in two styles, both of which are handled by this
library.
SWI-Prolog dicts provide a convenient and efficient alternative to option lists. For this reason, both built-in predicates and predicates that use this library support dicts transparantly.
Processing option lists inside time-critical code (loops) can cause
serious overhead. The above mentioned dicts is the preferred
mitigation. A more portable alternative is to define a record using
library(record)
and initialise this using make_<record>/2.
In addition to providing good performance, this also provides
type-checking and central declaration of defaults.
Options typically have exactly one argument. The library does support options with 0 or more than one argument with the following restrictions:
arg(1, Option, Default)
,
causing failure without arguments and filling only the first
option-argument otherwise.
Option | Term of the form Name(?Value). |
Options | is a list of Name(Value) or Name=Value
or a dict. |
?- option(max_depth(D), [x(a), max_depth(20)], 10). D = 20. ?- option(max_depth(D), [x(a)], 10). D = 10.
Option | Term of the form Name(?Value). |
Options | is a list of Name(Value) or Name=Value
or a dict. |
Multi-values options (e.g., proxy(Host, Port)
) are
allowed, where both option-name and arity define the identity of the
option.
call(IsMeta, Name)
.
Here is an example:
meta_options(is_meta, OptionsIn, Options), ... is_meta(callback).
Meta-options must have exactly one argument. This argument will be qualified.
name(V1,V2)
).
This is not allowed in dicts.
Also note that most system predicates and predicates using this library for processing the option argument can both work with classical Prolog options and dicts objects.