Re: enum sets

Liste des GroupesRevenir à l c 
Sujet : Re: enum sets
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.c
Date : 29. Aug 2024, 03: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/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

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