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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,976a050e0f89277c X-Google-Attributes: gid103376,public From: Mark A Biggar Subject: Re: Enumeration Types (was: Urgent question: malloc and ada) Date: 1998/04/13 Message-ID: <3532A14E.F0259CDA@lmco.com>#1/1 X-Deja-AN: 343757606 Content-Transfer-Encoding: 7bit References: <352A79C2.15FB7483@nathan.gmd.de> Content-Type: text/plain; charset=us-ascii Organization: Lockheed Martin Western Development Labs Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1998-04-13T00:00:00+00:00 List-Id: 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