Re: enum sets

Liste des GroupesRevenir à l c 
Sujet : Re: enum sets
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 29. Aug 2024, 09: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/>

Date Sujet#  Auteur
29 Aug 24 * enum sets29Thiago Adams
29 Aug 24 +* Re: enum sets4Keith Thompson
29 Aug 24 i+- Re: enum sets1Keith Thompson
29 Aug 24 i+- Re: enum sets1Kaz Kylheku
29 Aug 24 i`- Re: enum sets1Thiago Adams
29 Aug 24 +* Re: enum sets5David Brown
29 Aug 24 i+* Re: enum sets2Thiago Adams
29 Aug 24 ii`- Re: enum sets1Thiago Adams
29 Aug 24 i`* Re: enum sets2Blue-Maned_Hawk
29 Aug 24 i `- Re: enum sets1David Brown
29 Aug 24 +* Re: enum sets8fir
29 Aug 24 i+- Re: enum sets1fir
29 Aug 24 i+- Re: enum sets1fir
29 Aug 24 i+* Re: enum sets2fir
29 Aug 24 ii`- Re: enum sets1fir
29 Aug 24 i`* Re: enum sets3Thiago Adams
29 Aug 24 i `* Re: enum sets2fir
29 Aug 24 i  `- Re: enum sets1fir
29 Aug 24 +* Re: enum sets2Bonita Montero
29 Aug 24 i`- Re: enum sets1Thiago Adams
29 Aug 24 `* Re: enum sets9fir
29 Aug 24  +- Re: enum sets1fir
29 Aug 24  +* Re: enum sets6Thiago Adams
29 Aug 24  i`* Re: enum sets5fir
29 Aug 24  i `* Re: enum sets4fir
29 Aug 24  i  `* Re: enum sets3fir
29 Aug 24  i   `* Re: enum sets2fir
29 Aug 24  i    `- Re: enum sets1fir
29 Aug 24  `- Re: enum sets1fir

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal