Re: Enumerated data types

ok@goanna.cs.rmit.OZ.AU (Richard A. O'Keefe)
27 Aug 90 01:53:17 GMT

          From comp.compilers

Related articles
Enumerated data types mandel@forwiss.uni-passau.de (1990-08-23)
Re: Enumerated data types moss@cs.umass.edu (1990-08-24)
Re: Enumerated data types skrenta@amix.commodore.com (1990-08-15)
Re: Enumerated data types dik@cwi.nl (1990-08-24)
Re: Enumerated data types ok@goanna.cs.rmit.OZ.AU (1990-08-27)
Re: Enumerated data types jejones@microware.com (1990-08-27)
Re: Enumerated data types perelgut@turing.toronto.edu (1990-08-24)
Re: Enumerated data types dik@cwi.nl (1990-08-27)
Re: Enumerated data types grover@brahmand.Eng.Sun.COM (1990-08-28)
Re: Enumerated data types corbett@lupa.Eng.Sun.COM (1990-08-29)
Re: Enumerated data types pjj@cs.man.ac.uk (Pete Jinks) (1990-08-29)
[2 later articles]
| List of all articles for this month |
Newsgroups: comp.compilers
From: ok@goanna.cs.rmit.OZ.AU (Richard A. O'Keefe)
Keywords: C, Pascal, design, Ada
Organization: Comp Sci, RMIT, Melbourne, Australia
References: <1990Aug23.134826.2865@forwiss.uni-passau.de>
Date: 27 Aug 90 01:53:17 GMT

In article <1990Aug23.134826.2865@forwiss.uni-passau.de>, mandel@forwiss.uni-passau.de (Luis Mandel) writes:
[asks about re-using the names of enumeration constants]
[and how to make PRED and SUCC and so on work with them]


In a word: Ada.


Example from LRM 4.7:
type MASK is (FIX, DEC, EXP, SIGNIF);
type CODE is (FIX, CLA, DEC, TNZ, SUB);


M: MASK;
C: CODE;


PRINT(MASK'(DEC)); -- DEC is of type MASK
PRINT(CODE'(DEC)); -- DEC is of type CODE


for J in CODE'(FIX) .. CODE'(DEC) loop ... end loop;
-- one of the two bounds has to be qualified, the other needn't be:
for J in CODE'(FIX) .. DEC loop ... end loop;
for J in FIX .. CODE'(DEC) loop ... end loop;
-- or you can do it as a subrange
for J in CODE range FIX .. DEC loop ... end loop;


M := FIX; -- FIX is of type MASK
C := FIX; -- FIX is of type CODE


if M = DEC then ... end if; -- DEC is of type MASK
if C = DEC then ... end if; -- DEC is of type CODE
if CODE'(FIX) = CODE'(DEC) then ... end if
-- one of the two operands may omit the type qualifier


M := MASK'SUCC(M); -- uses the SUCC function proper to MASKs
C := CODE'SUCC(C); -- uses the SUCC function proper to CODEs


If one doesn't like using MASK' and CODE' all the time, one can say


function SUCC(X: MASK) return MASK renames MASK'SUCC;
function SUCC(X: CODE) return CODE renames CODE'SUCC;


and then
M := SUCC(M); -- uses MASK'SUCC
C := SUCC(C); -- uses CODE'SUCC


How does all this work? Overloading. Ada lets you have lots of different
things around with the same name, provided their types are sufficient to
tell them apart. Overloaded name resolution has been described often in
SigPlan Notices and the like.


Algol 68 was the first language I met that allowed overloading, but from
the published discussions of the Algol 68 committee overloading was already
a well known idea then. Anyone know where it first showed up? (PL/I had
"generic" functions, was that the first?) The new functional language
Haskell goes in for overloading in a big way.


The thing which made overloading tricky in Algol 68 was that Algol 68
combined overloading with automatic coercions. Ada doesn't go in for
automatic coercion.
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.