From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,INVALID_MSGID, PLING_QUERY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e0afec3ed133afd9 X-Google-Attributes: gid103376,public From: bobduff@world.std.com (Robert A Duff) Subject: Re: Enumerations and Arrays Unnecessary!??? Date: 1997/06/18 Message-ID: #1/1 X-Deja-AN: 251001002 References: <33A16AC0.1BA4@calvin.cca.rockwell.com> <01bc7b84$5fa9e300$LocalHost@xhv46.dial.pipex.com> Organization: The World Public Access UNIX, Brookline, MA Newsgroups: comp.lang.ada Date: 1997-06-18T00:00:00+00:00 List-Id: In article <01bc7b84$5fa9e300$LocalHost@xhv46.dial.pipex.com>, Nick Roberts wrote: >Class CHARACTER would have obvious subclasses, such as LETTER and DIGIT, >with their own specialised constants and methods (such as FIRST, LAST, >NEXT, and VALUE). This approach is really not so silly, if it could be >implemented efficiently (in a language other than Ada). I would insist on more than just efficiency: I also want the simple, concise notation, as in Ada where you just write down the list of literals, and the compiler handles the rest (e.g. making sure they are each assigned a unique code). I wouldn't want to laboriously define a new class for each of Red, Green, Yellow, etc, if the syntax for class definitions is longer than 2 tokens. Note that in most OO languages, the subclasses of a class aren't ordered in any particular way. But you often want enumeration literals to be ordered, so you can do "<" on them, and loop through them, and so forth. You also want to be able to have arrays indexed by enumeration types, which doesn't work in the subclass idea, unless you extend the notion of classes quite a bit. >I think this is an interesting question: could it be implemented >efficiently? It is possible to design a language such that this is efficient. >... In my ignorance, how does Smalltalk implement characters? And >other O. O. languages? Smalltalk has a Character class built in, which internally holds an integer number in the range 0..255. There is special syntax for character literals ($A in Smalltalk is like 'A' in Ada). You couldn't define your own character type in the same was as in Ada. You don't get subranges and case statements and so forth. You can't simply declare an array of Character and expect it to be efficient -- there's a special built-in String class. Character has no subclasses (that is, the letter A is not represented by its own class, but simply by an integer encoding). There are isLetter, isDigit, etc methods, rather than Letter, Digit, etc subclasses. You could define your own Color class, with values red, green, etc, in a similar manner. But there's nothing particularly object-oriented about it -- you're just assigning unique integer codes by hand. The "object-oriented" way would be to define classes Red, Green, etc as subclasses of Color. That's doable, but it's rather verbose if there are a lot of literals. Class Boolean is done this way. Another way to get something like enumeration literals in Smalltalk is to use Symbols. - Bob