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
Date: 13 Sep 93 14:24:24 GMT [thread overview]
Message-ID: <1993Sep13.142424.18799@mksol.dseg.ti.com> (raw)
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.)
next reply other threads:[~1993-09-13 14:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1993-09-13 14:24 john r strohm [this message]
-- strict thread matches above, loose matches on Subject: below --
1993-09-13 15:33 thoughts on "holey" enumerated types David Emery
1993-09-12 21:51 Michael Feldman
1993-09-12 21:45 Michael Feldman
1993-09-12 20:13 agate!howland.reston.ans.net!noc.near.net!inmet!bobduff
1993-09-12 18:02 agate!howland.reston.ans.net!usc!cs.utexas.edu!csc.ti.com!tilde.csc.ti.co
1993-09-11 23:23 cs.utexas.edu!utnut!utcsri!csri.toronto.edu!blaak
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox