From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b0d68c502c0ae6 X-Google-Attributes: gid103376,public From: Samuel Mize Subject: Re: Printing Enum Variable Re: Linux World Date: 1999/03/04 Message-ID: <7bmmu2$n0h@news1.newsguy.com> X-Deja-AN: 451282553 References: <7bfc2n$jl9@dfw-ixnews5.ix.netcom.com> <7bhh26$r7c$1@remarQ.com> <36DCAC1F.430E2C5E@aasaa.ofe.org> <7bk4v8$kl8$1@remarQ.com> <36DDA761.7B4E8099@aasaa.ofe.org> <7bkrmm$ao1$1@nnrp1.dejanews.com> <36DE0007.5236CEA2@aasaa.ofe.org> Organization: ImagiNet Communications, Ltd. User-Agent: tin/pre-1.4-981002 ("Phobia") (UNIX) (AIX/3-2) Newsgroups: comp.lang.ada Date: 1999-03-04T00:00:00+00:00 List-Id: David Starner wrote: > robert_dewar@my-dejanews.com wrote: >> True, but complaining about C for lack of enumeration >> *types* is a legitimate complaint. The named constants >> provided by C are an inadequate substitute. Enumeration >> types are an important abstraction, and their absence >> from C is a significant missing capability. >> >> Calling something an enum does not make it an enumeration >> type! In another message, you say: > No, it just means that a C enum is not quite the same abstraction as an > Ada enum. Both are true. You don't contradict Robert, but you missed his point. His point depends critically on understanding the concept of "type." This concept is fundamental to programming languages. A type is a set of legal values, and operations on those values. To say that a language provides the "enum" concept as a type, that language must ensure that a variable of that type will hold only those values, and it must provide the standard operations (like 'Last and 'First) for an enumeration. If it doesn't do this for you, it doesn't support the type as an entity of the language. In Ada, an enumeration is truly a type. In C (IIRC), an "enum" is just a bunch of names for numbers. So what? I mean, what's the difference, what's the advantage? Well, an example may make it clear. I don't recall the C syntax, so I'll speak here in general terms. In C, you can have an enumeration "color" of: RED = 0 WHITE = 1 BLUE = 2 and an enumeration "animal" of: DOG = 0 CAT = 1 FERRET = 2 GODZILLA = 3 If you have an integer variable Pet, which you intend to be of type "animal", there's nothing stopping you from writing: Pet = BLUE; /* should have written Pet_Color = BLUE; */ You can even write: Pet = 33; /* whoops, double-tapped the key */ Those are errors, and that kind of error occurs much more often than you would expect; especially when you're dealing with a large program with dozens of enumerations, all relating to (for instance) a windowing GUI system. Another example: a call to a windowing function that expects a window ID, an enumerated color (really, an integer), and a bitmap that covers some list of attributes (again, really an integer). In C, you might not notice that you reversed these two parameters, and there's no way the compiler can tell you: /* one of these must be right, the other wrong */ SetWindow (W1, color, attribs); SetWindow (W1, attribs, color); /* which one is the color? did the programmer get it right? */ SetWindow (W1, 0x3F, 0xA7); Pascal and Ada will catch the reversal of parameters on line 2 (or is it line 1? How do you know?) They will also keep you from manually typing in arbitrary integers. This annoys many C people, but again this is a common source of error in code that is maintained for more than one version. Even if the coder put the parameters in the right order, the codes may change. But the typed-in integers remain the same, forgotten in some seldom-used nook of the code until a user stumbles over it, causing a system crash. That's what "strong typing" is all about. It doesn't mean you should hit the keys more forcefully :-), it means that the type system built into the language knows a lot about what you're trying to do (if you use the type system to tell it), and it can help you write clearer code and avoid making mistakes. Elsewhere in this thread someone asked: > If you add an extra element to the enumeration, will the compiler > warn you about the switch statement? and you replied: > Yes, if you added a default case at the end of the switch statement. Well, no. The compiler doesn't care in either case. If you have a default case in your switch statement, you can detect at RUN TIME that you missed the new element. Or, as happens too often, the end user detects it, because the new element didn't get into the regression test suite at EVERY SINGLE POINT it was needed. In Ada, the compiler will stop you and make you code up that alternative, before you even start testing. Or not, if you used a "when others" alternative to provide a default behavior. This is less safe than explicitly listing all alternatives, but it can be a convenience, and it's safe if you're sure that the possible alternatives won't change (for instance, a case based on type type character). You use this -- if you're savvy -- knowing that you have lost part of the protection that a case statement usually provides. > Well, pulling out an old Pascal textbook, I don't see anything that > Pascal offers enumeration types that C doesn't. I hope you see now that the difference what C lets you do, often erroneously. You can do anything with an enumeration that you can do with any other integer. An enumeration value is just an integer, and an enumeration variable can hold any integer. Pascal and Ada assume you meant it when you said this type was an enumeration, and they detect typos and mental errors in enumeration usage. > I would hesitate to claim > that Pascal or C don't provide enumeration types just because they don't > offer all the dials and knobs that Ada does. Pascal offers enumeration types. C offers the concept of enumeration, but in a much weaker form, and it's not a true type (limited set of values + operations). > David Starner - OSU student - dstarner98@aasaa.ofe.org I hope you don't feel we're beating on you here. Keep asking questions, keep learning, God bless. Best, Sam Mize -- Samuel Mize -- smize@imagin.net (home email) -- Team Ada Fight Spam: see http://www.cauce.org/ \\\ Smert Spamonam