ConstEnum (const_enum v0.2.0)
View SourceThis module provides macros to define constants, which get compiled into functions.
To use this feature, this module must be included into the module using use/2.
use/2 will then compile a few things into the module:
- A list of constants (only available at compile time, to get it at runtime, you need to
add a function to retrieve the
:constantsattribute) - Add
before_compilehook - Constants docs chunks from
defconstforward/2included modules - Generate module docs with all constants, rendered as table by type,
in the module when giving the option
generate_docs: true, if the module already contains module docs, the constants docs will be appended to it
Constants can be annotated with the constdoc attribute to provide a documentation string for the constant type.
The ctypedoc attribute will set the type description (if types are generated and constdoc are not copied to the type).
The ctable attribute allows with false to disable generating a moduledoc table section for the type (any other value ignored).
The following functions and macros are created to work with the constants:
- Function
assert_name/2- Asserts that the constant exists (returns the name) as success tuple - Function
assert_name!/2- Asserts that the constant exists (returns the name) - Function
by_name/2,by_name/3- Returns the constant value by type and name as success tuple - Function
by_name!/2- Returns the constant value by type and name - Function
by_value/2,by_value/3- Returns the constant name by type and value as success tuple - Function
by_value!/2- Returns the constant name by type and value - Function
has_by_name/2- Checks whether the constant identified by type and name exists - Function
has_by_value/2- Checks whether the constant identified by type and value exists - Macro
macro_assert_name/2- Same asassert_name!/2but as macro - Macro
macro_by_name/2- Same asby_name!/2but as macro - Macro
macro_by_value/2- Same asby_value!/2but as macro
All bang functions will fail with RuntimeError (overridable with :exception),
if the constant does not exist. Likewise for macros.
Summary
Functions
The use hook to get started with defining constants (enums).
Define a constant by type, name and value.
Generates code to register additional constants through an Enumerable from the given application environment. The additional constants are registered to the defined type.
Define a constant forward to constants defined in another module.
Functions
The use hook to get started with defining constants (enums).
The following options are available:
copy_doc_to_type: boolean()- Optional. Copy the docs from the constant to the types.exception: module()- Optional. The exception module to use.generate_docs: boolean()- Optional. Generate documentation for each constant type. This will include generating types by default. Types have thetypeas their name. For values, this will be thetypewith the suffix_value.generate_types_value: boolean()- Optional. Generate types for not only the name, but also the values.ignore_duplicates: boolean()- Optional. Sets the pattern match clause as generated and thus will silence any warning about duplicate matches (i.e. duplicated name or value).no_types: boolean()- Optional. Do not generate any types.
Define a constant by type, name and value.
Generates code to register additional constants through an Enumerable from the given application environment. The additional constants are registered to the defined type.
The Enumerable elements must have an atom as key (for name) and any term as value (typically a non-negative integer for the BACnet protocol).
An optional validator function can be given to validate each new constant name and value. The first argument is the name and the second argument is the value.
Define a constant forward to constants defined in another module.
This will allow to use the functions and macros in this module to access the constants in the other module.