file
Enum.hCnxEnum provides macros for declaring algebraic datatypes called algebraic Enum
s, 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-styleenum
,TagType
, to tag/identify its variants. - #define match(x)
- Pattern matches on the given
Enum
,x
Pattern matching works similarly toswitch
statements, but with slightly different semantics. First off, pattern cases are declared withvariant(...)
, 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 theEnum
variant's members in place is not possible without manually doing so. Second, fallthrough is not possible, sobreak
ing 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 theEnum
variant's members in place is not possible without manually doing so. Unlikeswitch
statements, fallthrough is not possible inmatch
statements, sobreak
ing betweenvariant(...)
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. Unlikeswitch
statements, fallthrough is not possible inmatch
statements, sobreak
ing betweenvariant(...)
s is not necessary. - #define match_let(self, ...)
- Conditionally pattern matches on the given
Enum
,self
match_let
works similarly to anif
statement, but with slightly different semantics. First off, in addition to conditionally branching ifself
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 theEnum
variant's members in place is not possible without manually doing so. - #define is_variant(self, Variant)
- Determines if
self
is theEnum
variantVariant
- #define extract_variant(self, Variant)
- Extracts the internal value of
self
as the variantVariant
- #define variant_identifier(Variant)
- Generates the member identifier for an
Enum
variant,Variant
, used in the struct definition of anEnum
- #define ___ENUM_GET_TYPE_NAMES(...)
- extracts the
Enum
variant names from a list ofEnum
parameters - #define ___ENUM_CHECK_IS_TUPLE_AND_EXTRACT_TYPE(x)
- extract the
Enum
variant name from anEnum
parameter - #define ___ENUM_DEFINE_STRUCTS(...)
- Defines the structs used to represent
Enum
variants in anEnum
declaration. - #define ___ENUM_DEFINE_STRUCT(x)
- Defines a struct used to represent an
Enum
variant in anEnum
declaration. - #define ___ENUM_DEFINE_EMPTY_STRUCT(Name)
- Defines a struct used to represent an
Enum
variant in anEnum
declaration. - #define ___ENUM_DEFINE_STRUCT_IMPL(Name, x)
- Defines a struct used to represent an
Enum
variant in anEnum
declaration. - #define ___ENUM_EXPAND_STRUCT_IMPL(...)
- Defines a struct used to represent an
Enum
variant in anEnum
declaration. - #define ___ENUM_DEFINE_MEMBERS(...)
- Defines any additional members of an
Enum
in anEnum
declaration. - #define ___ENUM_DEFINE_MEMBER(x)
- Defines an additional members of an
Enum
in anEnum
declaration. - #define ___ENUM_BIND_VARIANT(variant_name, ...)
- Binds an
Enum
to a decomposition of the variantvariant_name
- #define ___ENUM_LET_BIND_VARIANT(self, variant_name, ...)
- Binds an
Enum
to a decomposition of the variantvariant_name