From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 12 Sep 93 21:45:24 GMT From: seas.gwu.edu!mfeldman@uunet.uu.net (Michael Feldman) Subject: Re: thoughts on "holey" enumerated types Message-ID: <1993Sep12.214524.13370@seas.gwu.edu> List-Id: In article <1993Sep11.192353.19703@jarvis.csri.toronto.edu> blaak@csri.toronto. edu (Raymond Blaak) writes: >The thread on converting integers to a sparse enumerated type has got >me thinking. > >Couldn't enumerated types always be implemented as a contiguous range, such >that use clauses to specify member ``positions'' just affected the 'POS and >'VAL functions? They _are_ usually so implemented by default. However, the USE repspec is very useful for mapping enumeration values to actual bit patterns in case this is necessary. The classic example is a device that requires exactly one "1" bit out of, say, 4, to represent a command, say, (Up, Down, Left, Right). One would then define TYPE Commands IS (Up, Down, Left, Right); FOR Commands USE (Up=>1, Down=>2, Left=>4, Right=>8); The device byte would somehow be "read" (memory-mapped IO, whatever). and no conversion would be necessary at all. More interesting is to allow _two_ mappings: TYPE Commands IS (Up, Down, Left, Right); -- default used within program -- 0 1 2 3 (probably, but we don't care) TYPE ExtCommands IS NEW Commands; -- type derivation FOR ExtCommands USE (Up=>1, Down=>2, Left=>4, Right=>8); Now: how do we get from one to the other? This is simpler than many realize: suppose we have C: Commands; E: ExtCommands; . . . C := Commands(E): E := ExtCommands(C); in other words, a straightforward explicit conversion, does the trick. This is suggested in the Ada Rationale. I have checked it on perhaps a dozen compilers and found that it works as I have just suggested. The bottom line: no hacking, Unchecked_Conversion, or anything else on the part of the programmer is needed. The type system - in Ada83 - does it all. If you think you can beat the compiler's conversion method, you're surely welcome to try... > >That way enumerated type operations (especially loop traversal over a range, >or array indexing) can always be efficient. The only time one needs to know a >member's position is when converting to an integer. Using 'POS and 'VAL seems >safer than unchecked_conversion in these cases anyway. It's not even clear to me why people insist on doing these conversions, when there are usually easier ways. My guess is that the loop you suggest is done on the _positions_ anyway, not on the values; why would it be otherwise? All the repspec should do is cause the compiler to create a little table of the mapping. So what is the problem? In my next message I will re-post a little compilable program illustrating this. Understanding and running it will probably give you some insight; I know it opened _my_ eyes, especially when I ran it under several compilers. Mike Feldman ------------------------------------------------------------------------ Michael B. Feldman - co-chair, SIGAda Education Committee Professor, Dept. of Electrical Engineering and Computer Science The George Washington University - Washington, DC 20052 USA 202-994-5253 (voice) - 202-994-0227 (fax) - mfeldman@seas.gwu.edu (Internet) "We just changed our CONFIG.SYS, then pressed CTRL-ALT-DEL. It was easy." -- Alexandre Giglavyi, director Lyceum of Information Technologies, Moscow. ------------------------------------------------------------------------