comp.lang.ada
 help / color / mirror / Atom feed
From: Samuel Mize <smize@imagin.net>
Subject: Re: Printing Enum Variable Re: Linux World
Date: 1999/03/04
Date: 1999-03-04T00:00:00+00:00	[thread overview]
Message-ID: <7bmmu2$n0h@news1.newsguy.com> (raw)
In-Reply-To: 36DE0007.5236CEA2@aasaa.ofe.org

David Starner <dstarner98@aasaa.ofe.org> 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




  parent reply	other threads:[~1999-03-04  0:00 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-03-02  0:00 Linux World Richard D Riehle
1999-03-02  0:00 ` fraser
1999-03-02  0:00   ` Printing Enum Variable " David Starner
1999-03-03  0:00     ` Larry Kilgallen
1999-03-03  0:00       ` Nick Roberts
1999-03-03  0:00         ` David Starner
1999-03-03  0:00     ` Fraser Wilson
1999-03-03  0:00       ` David Starner
1999-03-04  0:00         ` Magnus Larsson
1999-03-03  0:00           ` Hans Marqvardsen
1999-03-04  0:00             ` Nick Roberts
1999-03-04  0:00             ` robert_dewar
1999-03-04  0:00               ` Hans Marqvardsen
1999-03-05  0:00                 ` dewar
1999-03-07  0:00                   ` Hans Marqvardsen
1999-03-04  0:00               ` Hans Marqvardsen
1999-03-05  0:00                 ` Larry Kilgallen
1999-03-05  0:00                 ` dewar
1999-03-05  0:00                   ` David Botton
1999-03-05  0:00                     ` robert_dewar
1999-03-03  0:00           ` Printing Enum Variable Re: Linux World (Correction) Hans Marqvardsen
1999-03-04  0:00         ` Printing Enum Variable Re: Linux World fraser
1999-03-05  0:00           ` Nick Roberts
1999-03-05  0:00             ` fraser
1999-03-04  0:00         ` Richard D Riehle
1999-03-04  0:00         ` Richard D Riehle
1999-03-04  0:00         ` robert_dewar
1999-03-03  0:00     ` fraser
1999-03-03  0:00       ` David Starner
1999-03-03  0:00         ` Samuel T. Harris
1999-03-03  0:00         ` fraser
1999-03-04  0:00         ` dennison
1999-03-04  0:00           ` Ehud Lamm
1999-03-04  0:00         ` robert_dewar
1999-03-03  0:00           ` David Starner
1999-03-04  0:00             ` robert_dewar
1999-03-04  0:00             ` Samuel Mize [this message]
1999-03-04  0:00               ` Samuel Mize
1999-03-05  0:00                 ` Robert A Duff
1999-03-05  0:00               ` Robert A Duff
1999-03-07  0:00               ` Florian Weimer
1999-03-07  0:00                 ` Michael Young
1999-03-07  0:00                   ` Matthew Heaney
1999-03-08  0:00                     ` Michael Young
1999-03-08  0:00                       ` Matthew Heaney
1999-03-07  0:00                   ` Larry Kilgallen
1999-03-07  0:00                     ` Michael Young
1999-03-08  0:00                       ` Larry Kilgallen
1999-03-08  0:00                         ` robert_dewar
1999-03-08  0:00                       ` Florian Weimer
1999-03-08  0:00                       ` robert_dewar
1999-03-08  0:00                         ` Richard D Riehle
1999-03-09  0:00                           ` Michael Young
1999-03-09  0:00                             ` Larry Kilgallen
1999-03-09  0:00                               ` billy
1999-03-10  0:00                                 ` Pascal Obry
1999-03-10  0:00                                 ` robert_dewar
1999-03-10  0:00                                   ` Dale Stanbrough
1999-03-10  0:00                                     ` dennison
1999-03-10  0:00                                       ` bob
1999-03-10  0:00                                         ` Mike Silva
1999-03-10  0:00                                           ` dennison
1999-03-10  0:00                                           ` Nick Roberts
1999-03-10  0:00                                     ` Richard D Riehle
1999-03-10  0:00                                     ` Pascal Obry
1999-03-09  0:00                               ` Michael Young
1999-03-10  0:00                                 ` Mike Silva
     [not found]                               ` <7 <7c58qa$b6b$1@cf01.edf.fr>
1999-03-10  0:00                                 ` fraser
     [not found]                               ` <7c4ru6$e45$1@remarq.com>
1999-03-10  0:00                                 ` fraser
     [not found]                             ` <1999Mar9.131659. <dale-1003991644340001@r1021c-07.ppp.cs.rmit.edu.au>
1999-03-10  0:00                               ` Larry Kilgallen
1999-03-04  0:00           ` Ehud Lamm
1999-03-05  0:00             ` Richard D Riehle
1999-03-05  0:00               ` Nick Roberts
1999-03-06  0:00                 ` Ehud Lamm
1999-03-06  0:00                   ` robert_dewar
1999-03-06  0:00                     ` Larry Kilgallen
1999-03-06  0:00                       ` Dave Taylor
1999-03-06  0:00                         ` Bruce or Tracy
1999-03-06  0:00                     ` Ehud Lamm
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox