Deutsch English Français Italiano |
<vapmcd$3ullf$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: Re: enum sets Date: Thu, 29 Aug 2024 08:34:36 -0300 Organization: A noiseless patient Spider Lines: 95 Message-ID: <vapmcd$3ullf$2@dont-email.me> References: <vaoclb$3lfbf$1@dont-email.me> <vap88m$3ssqh$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 29 Aug 2024 13:34:37 +0200 (CEST) Injection-Info: dont-email.me; posting-host="f80b8b85f3cf443035f3bd640f6489b4"; logging-data="4150959"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+E2Tp2b0hiGbg3KyPqDZmRhNt4O4YbWH0=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:b7nyYPXmpkYOn4hVo0Cs4QGFnF8= Content-Language: en-US In-Reply-To: <vap88m$3ssqh$1@dont-email.me> Bytes: 3151 On 29/08/2024 04:33, David Brown wrote: > 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; > One of my motivations was that I have a big enum and sometimes I need to check in switch cases all values of some type. For instance, let's say enum E {A, B, C, D /*until Z*/}; And I need to check a subset for instance A, B To have a warning if I miss some item I need to include all cases. void f(enum E e) { switch(e) { case A: case B: break; case C: //..Z break; } } //warning you missing D case. The problem this is not practical when the number of items is too big. Using default:break; makes it practical but then the warning disappears. With this enum set idea the code would be enum E { enum Subset {A, B} , C, D /*until Z*/ }; void f(enum Subset e) { switch(e) { case A: break; } //warning you miss case B }