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: 13 Sep 93 14:24:24 GMT From: agate!howland.reston.ans.net!wupost!cs.utexas.edu!csc.ti.com!tilde.csc.ti .com!mksol!strohm@ucbvax.Berkeley.EDU (john r strohm) Subject: Re: thoughts on "holey" enumerated types Message-ID: <1993Sep13.142424.18799@mksol.dseg.ti.com> List-Id: In article <1993Sep12.214524.13370@seas.gwu.edu> mfeldman@seas.gwu.edu (Michael Feldman) writes: >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 catch is that the conversion from ExtCommands (1-of-4) to Commands involves a table search operation, and there is no general way around it. (This particular case COULD be handled by a really smart compiler recognizing the 1-of-4 encoding. Consider a spec that used 2,3,5,7.)