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,c3b704caaa76c7c5 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-12-18 09:53:21 PST Newsgroups: comp.lang.ada Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!nntp.abs.net!uunet!dca.uu.net!ash.uu.net!world!news From: Robert A Duff Subject: Re: Constants instead of enum? User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Sender: news@world.std.com (Mr Usenet Himself) Message-ID: Date: Wed, 18 Dec 2002 17:52:03 GMT Content-Type: text/plain; charset=us-ascii References: <5ad0dd8a.0212180703.71f5b6b7@posting.google.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Organization: The World Public Access UNIX, Brookline, MA Xref: archiver1.google.com comp.lang.ada:32035 Date: 2002-12-18T17:52:03+00:00 List-Id: wojtek@power.com.pl (Wojtek Narczynski) writes: > Hello, > > I have the following situation: I read request record from network, an > Unsigned_8 (byte) denotes its type. Valid types are 1..10. Response > record has the same structure as request record. I also want to reuse > the memory allocated for request as the response. Now problem: for > every unimplemented request type (outside 1..10 range) I am supposed > to send a response record with type 11 back. > > I am trying to use enumeration with representation clause for this, > but it just doesn't work. Correct -- it doesn't work. >... I read an Unsigned_8 from network, and all > its values are valid and should be handled gracefully, so I cannot use > this enum in the request structure. Because of this I have to convert > from Unsigned_8 to enumeration, but only sometimes. Then I have to use > unchecked conversion to get the enumeration's underlying Unsigned_8 to > store it in the response record (same structure and memory) as > request, so it has to be Unsigned_8. > > If I defined types as constants for 1..11 range the amount of code > necessary and conversions will reduce. > > So my question is - would this be fine to use a group of consts in > this specific case instead of an enum? I could encapsulate them into a > nested package. This would be fine. In fact, it is superior to the enum-with-rep-clause solution, for the reasons you stated. In fact, I would go so far as to say enum rep clauses are rarely useful, and should not be in the language. Use enums when you don't care about the representation. If you care about representation, use types that exactly match the hardware or whatever you're interfacing to, and use constants (perhaps deferred constants) to define enum-like values. - Bob