Sujet : Re: enum sets
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 29. Aug 2024, 02:31:40
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240828181353.861@kylheku.com>
References : 1 2
User-Agent : slrn/pre1.0.4-9 (Linux)
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/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca