Sujet : Re: enum sets
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 29. Aug 2024, 08:33:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vap88m$3ssqh$1@dont-email.me>
References : 1
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 29/08/2024 01:42, Thiago Adams wrote:
I am wondering how useful would be to have enum sets.
Let´s say you have a function that accepts only monospaced fonts.Then you can use enum monospaced_font_type. Or a switch case where you need to check all and only monospaced_font_type.
But at same type you can store at same object monospaced_font_type or font_type.
enum font_type
{
enum monospaced_font_type
{
CASCADIA_FONT,
},
ARIAL_FONT
};
This could be arranged in any way.
I think this could have some use-cases, but I suspect that often you would want to have separate enumerations defined first, then combine them with a sum type (aka variant, tagged union, discriminated type, etc.).
enum monospaced_font_type {
cascadia
};
enum proportional_font_type {
arial
};
typedef struct {
enum { is_monospaced_font_type, is_proportional_font_type } tag;
union {
enum monospaced_font_type mf;
enum proportional_font_type pf;
}
} font_type;
This also makes it more efficient for a function to determine which sub-enum type a given font is. Unfortunately, C does not have good support or syntax for sum types - it all has to be handled manually and with either extra wrapping or no type safety. C++ does a bit better, with enum classes and std::variant<>, but these are later additions to the language and much less elegant than the sum types and pattern matching found in a wide range of other languages.
One possible source of inspiration for adding features to a C-like language is Cyclone, which was an attempt at a safer and better C. Given that it is 20 years old and I only read about it recently, I guess it never took off. But it could give you ideas.
<
http://cyclone.thelanguage.org/wiki/Tagged%20Unions/>