Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!wuarchive!husc6!spdcc!ima!esegue!compilers-sender From: ok@goanna.cs.rmit.OZ.AU (Richard A. O'Keefe) Newsgroups: comp.compilers Subject: Re: Enumerated data types Keywords: C, Pascal, design, Ada Message-ID: <3621@goanna.cs.rmit.oz.au> Date: 27 Aug 90 01:53:17 GMT References: <1990Aug23.134826.2865@forwiss.uni-passau.de> Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: ok@goanna.cs.rmit.OZ.AU (Richard A. O'Keefe) Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 60 Approved: compilers@esegue.segue.boston.ma.us 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. -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.