include/Cnx/Enum.h file

CnxEnum provides macros for declaring algebraic datatypes called algebraic Enums, similar to Rust's Enum, and pattern matching on their variants.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defines

#define Enum(Type, ...)
Declares and defines an Enum, Type, with the given variants and additional members.
#define EnumWithTag(Type, TagType, ...)
Declares and defines an Enum, Type, using an existing C-style enum, TagType, to tag/identify its variants.
#define match(x)
Pattern matches on the given Enum, x Pattern matching works similarly to switch statements, but with slightly different semantics. First off, pattern cases are declared with variant(...), which can, in addition to match the variant, optionally decompose the variant into one or more bound variables. Bound variables are bound by value, as decomposing a variant during pattern matching is intended to be consuming, so mutating the Enum variant's members in place is not possible without manually doing so. Second, fallthrough is not possible, so breaking to prevent it is not necessary.
#define variant(...)
Declares a pattern-matching case in a match statement Pattern-matching cases declare a variant to match against and, in addition, can optionally decompose the variant into one or more bound variables. Bound variables are bound by value, as decomposing a variant during pattern matching is intended to be consuming, so mutating the Enum variant's members in place is not possible without manually doing so. Unlike switch statements, fallthrough is not possible in match statements, so breaking between variant(...)s is not necessary.
#define wildcard()
Declares a catch-all pattern-matching case in a match statement This will match all variants that have not been explicitly matched against prior. Unlike switch statements, fallthrough is not possible in match statements, so breaking between variant(...)s is not necessary.
#define match_let(self, ...)
Conditionally pattern matches on the given Enum, self match_let works similarly to an if statement, but with slightly different semantics. First off, in addition to conditionally branching if self is the given variant, it can optionally decompose the variant into one or more bound variables. Bound variables are bound by value, as decomposing a variant during pattern matching is intended to be consuming, so mutating the Enum variant's members in place is not possible without manually doing so.
#define is_variant(self, Variant)
Determines if self is the Enum variant Variant
#define extract_variant(self, Variant)
Extracts the internal value of self as the variant Variant
#define variant_identifier(Variant)
Generates the member identifier for an Enum variant, Variant, used in the struct definition of an Enum
#define ___ENUM_GET_TYPE_NAMES(...)
extracts the Enum variant names from a list of Enum parameters
#define ___ENUM_CHECK_IS_TUPLE_AND_EXTRACT_TYPE(x)
extract the Enum variant name from an Enum parameter
#define ___ENUM_DEFINE_STRUCTS(...)
Defines the structs used to represent Enum variants in an Enum declaration.
#define ___ENUM_DEFINE_STRUCT(x)
Defines a struct used to represent an Enum variant in an Enum declaration.
#define ___ENUM_DEFINE_EMPTY_STRUCT(Name)
Defines a struct used to represent an Enum variant in an Enum declaration.
#define ___ENUM_DEFINE_STRUCT_IMPL(Name, x)
Defines a struct used to represent an Enum variant in an Enum declaration.
#define ___ENUM_EXPAND_STRUCT_IMPL(...)
Defines a struct used to represent an Enum variant in an Enum declaration.
#define ___ENUM_DEFINE_MEMBERS(...)
Defines any additional members of an Enum in an Enum declaration.
#define ___ENUM_DEFINE_MEMBER(x)
Defines an additional members of an Enum in an Enum declaration.
#define ___ENUM_BIND_VARIANT(variant_name, ...)
Binds an Enum to a decomposition of the variant variant_name
#define ___ENUM_LET_BIND_VARIANT(self, variant_name, ...)
Binds an Enum to a decomposition of the variant variant_name