Deutsch English Français Italiano |
<20240828181353.861@kylheku.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.c Subject: Re: enum sets Date: Thu, 29 Aug 2024 01:31:40 -0000 (UTC) Organization: A noiseless patient Spider Lines: 55 Message-ID: <20240828181353.861@kylheku.com> References: <vaoclb$3lfbf$1@dont-email.me> <871q28b26c.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 29 Aug 2024 03:31:40 +0200 (CEST) Injection-Info: dont-email.me; posting-host="ee676becc5cc43d653dc7d1580a96201"; logging-data="3876075"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195nejsfP2Ac52sInDYV4okEC2qYI2GhrY=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:ja9qy/hEm1j2ZGX5jDP1kvmqT+8= Bytes: 2866 On 2024-08-29, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > Thiago Adams <thiago.adams@gmail.com> writes: >> 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. > > If I understand you correctly, enum monospaced_font_type is a *subtype* > of enum font_type. > > Ada has something very similar to this: > > type Font_Type is (CASCADIA_FONT, ARIAL_FONT); > subtype monospaced_font_type is Font_type range CASCADIA_FONT .. CASCADIA_FONT; > > An Ada subtype is a type with an optionally added *constraint*, > which can be checked at runtime. But it's hard to see how you'd > add this to C. > > Given your type definition, how would this behave? > > void func(enum monospaced_font_type); > enum font_type font = ARIAL_FONT; > func(font); If enums are to be "safe", the only treatment that makes sense is a constraint violation (overridable with a cast). The reverse conversion would not violate a constraint. Run-time type information in enumeration tags seems like a wrong stacking of abstractions. Enumerations are something with the help of which you can implement dynamic typing in a run-time written in C; you really just want them to be "dumb integers", with some compile-time checks that catch bugs more often than they don't. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca