comp.lang.ada
 help / color / mirror / Atom feed
From: Mark A Biggar <mark.a.biggar@lmco.com>
Subject: Re: Enumeration Types (was: Urgent question: malloc and ada)
Date: 1998/04/13
Date: 1998-04-13T00:00:00+00:00	[thread overview]
Message-ID: <3532A14E.F0259CDA@lmco.com> (raw)
In-Reply-To: matthew_heaney-ya023680001204981840210001@news.ni.net


Matthew Heaney wrote:
> In practice, using an integer type instead of an enumeration is only
> marginally less safe.  Consider this:
> 
> type ET is (Red, Green, Blue);
> for ET use (Red => 1, Green => 3, Blue => 7);
> for ET'Size use 8;
> 
> procedure Op (O : ET);
> 
> Now consider this:
> 
> type IT is new Interfaces.Unsigned_8;
> 
> Red : constant IT := 1;
> Green : constant IT := 3;
> Blue : constant IT := 7;
> 
> procedure Op (O : IT);
> 
> From a client's point of view, there is no difference between these types
> (though a case statement now requires an others branch).  You call
> procedure Op the same way in both cases.
> 
> You can make the argument, "But with the integer version, the client can
> pass the value 2."  But this can only happen because of deliberate misuse,
> ie
> 
> Op (2);
> 
> Here, he's being naughty, and using an integer literal.  What he should do is
> 
> Op (Red);
> 
> just like he'd do using the enumeration literal.
> 
> And no, you can't even accidently mix this type up with other integer
> types, because IT is its own type.  You'd have to deliberately convert one
> integer type into the other.

Actually you can do even better as follows:

package SomePackage is
 
 type IT is private;
 
 Red : constant IT;
 Green : constant IT;
 Blue : constant IT;
 
 procedure Op (O : IT);

private
 
 type IT is new Interfaces.Unsigned_8;
 
 Red : constant IT := 1;
 Green : constant IT := 3;
 Blue : constant IT := 7;

end SomePackage;

This eliminates the Op(2) problem while keeping all the properties of your
example.

--
Mark Biggar
mark.a.biggar@lmco.com




  reply	other threads:[~1998-04-13  0:00 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-04-07  0:00 Urgent question: malloc and ada Guido Tesch
1998-04-09  0:00 ` Joe Gwinn
1998-04-10  0:00   ` Robert Dewar
1998-04-12  0:00     ` Joe Gwinn
1998-04-12  0:00       ` Enumeration Types (was: Urgent question: malloc and ada) Matthew Heaney
1998-04-13  0:00         ` Mark A Biggar [this message]
1998-04-10  0:00   ` Urgent question: malloc and ada...READ/NEW/FOLLOWUP Larry Kilgallen
1998-04-12  0:00     ` Joe Gwinn
1998-04-14  0:00       ` Robert Dewar
1998-04-16  0:00         ` Joe Gwinn
1998-04-17  0:00           ` Robert Dewar
1998-04-18  0:00             ` Joe Gwinn
1998-04-18  0:00               ` Robert Dewar
1998-04-25  0:00                 ` Joe Gwinn
1998-04-26  0:00                   ` Robert Dewar
1998-04-26  0:00                     ` Jonathan Guthrie
1998-04-26  0:00                       ` Simon Wright
1998-04-26  0:00                         ` Larry Kilgallen
1998-04-27  0:00                           ` Stephen Leake
1998-04-26  0:00                       ` Robert Dewar
1998-04-30  0:00                       ` Robert I. Eachus
1998-04-30  0:00                         ` Urgent question: malloc and ada Larry Kilgallen
     [not found]                           ` <matthew_heaney-ya023680003004981723000001@news.ni.net>
1998-05-01  0:00                             ` Robert Dewar
1998-05-01  0:00                               ` Larry Kilgallen
1998-05-02  0:00                                 ` Robert Dewar
1998-05-02  0:00                                   ` Jonathan Guthrie
1998-05-03  0:00                                     ` Robert Dewar
1998-05-03  0:00                                     ` Richard Kenner
1998-05-03  0:00                                       ` Jonathan Guthrie
1998-05-03  0:00                                         ` Robert Dewar
1998-05-03  0:00                                         ` Richard Kenner
1998-05-03  0:00                                   ` Larry Kilgallen
1998-05-01  0:00                           ` Robert Dewar
1998-05-01  0:00                             ` Charles Hixson
1998-05-02  0:00                               ` Jonathan Guthrie
1998-05-02  0:00                             ` Corey Minyard
1998-05-06  0:00                             ` Robert I. Eachus
1998-05-12  0:00                               ` Craig T. Spannring
1998-05-12  0:00                                 ` Robert Dewar
1998-05-01  0:00                       ` Urgent question: malloc and ada...READ/NEW/FOLLOWUP Fergus Henderson
1998-05-01  0:00                     ` Joe Gwinn
1998-05-02  0:00                       ` Larry Kilgallen
1998-05-05  0:00                       ` Robert Dewar
replies disabled

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