comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@world.std.com (Robert A Duff)
Subject: Re: Enumerations and Arrays Unnecessary!???
Date: 1997/06/18
Date: 1997-06-18T00:00:00+00:00	[thread overview]
Message-ID: <EByzCB.tr@world.std.com> (raw)
In-Reply-To: 01bc7b84$5fa9e300$LocalHost@xhv46.dial.pipex.com


In article <01bc7b84$5fa9e300$LocalHost@xhv46.dial.pipex.com>,
Nick Roberts <Nick.Roberts@dial.pipex.com> 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




  reply	other threads:[~1997-06-18  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-13  0:00 Enumerations and Arrays Unnecessary!??? JR Crosmer
1997-06-14  0:00 ` Robert Dewar
1997-06-16  0:00   ` Fanaticism [was Re: Enumerations and Arrays Unnecessary!???] Jan Galkowski
1997-06-14  0:00 ` Enumerations and Arrays Unnecessary!??? Robert A Duff
1997-06-14  0:00   ` Robert Dewar
1997-06-18  0:00     ` Nick Roberts
1997-06-18  0:00       ` Robert A Duff [this message]
1997-06-16  0:00 ` David Wheeler
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox