Add regex types to types module
Current state
In the types module the type of objects can be checked. This is not restricted to Prolog's definition of types: at least for dicts required and optional keys (with type restrictions) may be defined.
Suggestion
The regex type would extend this idea to strings: the value must not only be of type string but its content must satisfy additional requirements. Additionally, a regex type for atom
s or general text
is desired.
Rationale
We have decided, that absolute paths of files and directories may not end in a slash unless it is the root directory '/'. Without the regex type, this restriction must be enforced after type checking. However for this, a mechanisms very similar to type checking are required. For example in set_bg
:
- if we have a single dict:
- noting which values are concerned (probably only
abs_path
), - extracting the value
- checking the regex
- if the check fails: throw exception
- noting which values are concerned (probably only
- if we have a list of dicts:
- iterate over all elements
- if we then have a dict, apply the steps from above
It is easier, less error prone, and more encapsulated to include this facility into types.
Technical realisation
The actual checking of the type might be simple as there is a regex library in Prolog. Taking strings as example, the type definition could look like
-
regex(string, RegEx)
whereRegEx
must be a valid regular expression from Prolog's regex library or -
string(RegEx)
whereRegEx
is defined as above.
The second option would convey the notion that this is a refinement of the string type more strongly. This is analogous to dict
vs. dict/1
and dict/2
and list
vs list/1
already realized in types
.