comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <Nick.Roberts@dial.pipex.com>
Subject: Re: Printing Enum Variable Re: Linux World
Date: 1999/03/05
Date: 1999-03-05T00:00:00+00:00	[thread overview]
Message-ID: <7bpn1v$3kd$1@plug.news.pipex.net> (raw)
In-Reply-To: 7bpevu$frn@dfw-ixnews12.ix.netcom.com

Richard D Riehle wrote in message <7bpevu$frn@dfw-ixnews12.ix.netcom.com>...
[...]
| Enumeration types also carry the potential for abuse.  I recall
| a programmer who had a package specification with an enumeration
| type where the set of values spanned three pages of source code.


I think it's interesting to note that the (notional) declaration of the
Character type (RM95 A.1) is pretty big, and a declaration of Wide_Character
would span more than three pages!  I suppose it's also interesting to note
that these types can't be declared by a normal Ada declaration (the control
characters are unrepresentable).

| If enumeration types are to be on the idiom list, they should be
| accompanied by a set of guidelines.  One of the problems we are
| trying to solve with object-oriented programming and child library
| units is the extension of existing code without performing "open
| heart surgery" on it.  Heavy use of enumeration types can result in
| cracking open existing code to perform such extensions.
|
| Some of the guidelines I follow, and counsel my clients to follow,
| 1) only use enumeration types when there is a well-known limit to the
| set of values.  2) Use them when there is a clear requirement for an
| ordered set, 3) Use them for identifying types that have a small
| set of values (e.g., Location is (Front, Back);).


The canonical example -- I suppose -- being the type Boolean.

| There is a point of view in the OOP community that says that enumeration
| types are an artifact of an obsolete style of programming.  That is,
| an enumeration type, because it is not extensible and cannot be
| further specialized, is antithetical to OOP.  Although I find this
| viewpoint a little too narrow for my taste, one could make a very
| good case to support it in many situations.


In Ada, tagged types -- possibly empty -- can be used like extensible
enumeration types (which is presumably partly why the original idea of
extensible enumeration types was rejected).  The syntax is, perhaps,
slightly clumsy for this use, sometimes.

Compare:

   type Traffic_Light_State is (Red, Amber, Green, Red_And_Amber);

   Traffic_May_Pass: array (Traffic_Light_State) of Boolean :=
      (Red | Red_And_Amber => False, Amber | Green => True);

If you wanted to added a new state (blinking amber, say), you would have
difficulty in two ways: firstly, you can't extend the enumerated type
without performing 'open heart surgery' on the code; secondly, the question
of whether traffic may pass may need to be qualified (by whether there is a
pedestrian crossing the road), so that you can't just extend the array, you
have to make some more extensive changes to the program's logic.  Now
consider:

   type Traffic_Light_State is abstract tagged ...;

   function Traffic_May_Pass (State: in Traffic_Light_State) return Boolean
is abstract;

then:

   type Red_Light_State is new Traffic_Light_State with ...;

   function Traffic_May_Pass (State: in Red_Light_State) return Boolean;

where the function body would presumably just return False, and so on for
the other states.  Now, a new state can be added easily, and it may be
possible to add logic in the body of the Traffic_May_Pass function to decide
whether a pedestrian is crossing the road or not before giving traffic
permission to pass.

However, it remains difficult to code, in an easily extensible way, which
state comes first or last (as in the attributes First and Last), or to
iterate over them (as in the attributes Pred and Succ), since extensibility
means there is no clear 'last' element, and no clear 'next' for each one.

Like most good ideas, this idea is ludicrous when taken to the extreme.
Would it make sense to implement Boolean as a tagged type?  Or Character, or
Wide_Character?  (Perhaps it _would_ make sense for Wide_Character or a
similar type, but I'm dubious.)

-------------------------------------
Nick Roberts
-------------------------------------







  reply	other threads:[~1999-03-05  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     ` fraser
1999-03-03  0:00       ` David Starner
1999-03-03  0:00         ` fraser
1999-03-03  0:00         ` Samuel T. Harris
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
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                   ` Larry Kilgallen
1999-03-07  0:00                     ` Michael Young
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                                           ` Nick Roberts
1999-03-10  0:00                                           ` dennison
1999-03-10  0:00                                     ` Pascal Obry
1999-03-10  0:00                                     ` Richard D Riehle
1999-03-09  0:00                               ` Michael Young
1999-03-10  0:00                                 ` Mike Silva
     [not found]                               ` <7c4ru6$e45$1@remarq.com>
1999-03-10  0:00                                 ` fraser
     [not found]                               ` <7 <7c58qa$b6b$1@cf01.edf.fr>
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-08  0:00                       ` Florian Weimer
1999-03-08  0:00                       ` Larry Kilgallen
1999-03-08  0:00                         ` robert_dewar
1999-03-07  0:00                   ` Matthew Heaney
1999-03-08  0:00                     ` Michael Young
1999-03-08  0:00                       ` Matthew Heaney
1999-03-04  0:00           ` Ehud Lamm
1999-03-05  0:00             ` Richard D Riehle
1999-03-05  0:00               ` Nick Roberts [this message]
1999-03-06  0:00                 ` Ehud Lamm
1999-03-06  0:00                   ` robert_dewar
1999-03-06  0:00                     ` Ehud Lamm
1999-03-06  0:00                     ` Larry Kilgallen
1999-03-06  0:00                       ` Dave Taylor
1999-03-06  0:00                         ` Bruce or Tracy
1999-03-04  0:00         ` dennison
1999-03-04  0:00           ` Ehud Lamm
1999-03-03  0:00     ` Fraser Wilson
1999-03-03  0:00       ` David Starner
1999-03-04  0:00         ` fraser
1999-03-05  0:00           ` Nick Roberts
1999-03-05  0:00             ` fraser
1999-03-04  0:00         ` Magnus Larsson
1999-03-03  0:00           ` Printing Enum Variable Re: Linux World (Correction) Hans Marqvardsen
1999-03-03  0:00           ` Printing Enum Variable Re: Linux World 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                 ` Larry Kilgallen
1999-03-05  0:00                 ` dewar
1999-03-05  0:00                   ` David Botton
1999-03-05  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         ` Richard D Riehle
1999-03-04  0:00         ` Richard D Riehle
1999-03-04  0:00         ` robert_dewar
1999-03-03  0:00     ` Larry Kilgallen
1999-03-03  0:00       ` Nick Roberts
1999-03-03  0:00         ` David Starner
replies disabled

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